version4.go 911 B

12345678910111213141516171819202122232425
  1. // Copyright 2011 Google Inc. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package uuid
  5. // Random returns a Random (Version 4) UUID or panics.
  6. //
  7. // The strength of the UUIDs is based on the strength of the crypto/rand
  8. // package.
  9. //
  10. // A note about uniqueness derived from from the UUID Wikipedia entry:
  11. //
  12. // Randomly generated UUIDs have 122 random bits. One's annual risk of being
  13. // hit by a meteorite is estimated to be one chance in 17 billion, that
  14. // means the probability is about 0.00000000006 (6 × 10−11),
  15. // equivalent to the odds of creating a few tens of trillions of UUIDs in a
  16. // year and having one duplicate.
  17. func NewRandom() UUID {
  18. uuid := make([]byte, 16)
  19. randomBits([]byte(uuid))
  20. uuid[6] = (uuid[6] & 0x0f) | 0x40 // Version 4
  21. uuid[8] = (uuid[8] & 0x3f) | 0x80 // Variant is 10
  22. return uuid
  23. }