create NewRandom func consistent with other version constructors
The New function is kept for convenience and relies on the NewRandom
function.
diff --git a/version4.go b/version4.go
index 913d019..0f1a72c 100644
--- a/version4.go
+++ b/version4.go
@@ -4,7 +4,17 @@
package uuid
-// New returns a Random (Version 4) UUID or panics.
+import "io"
+
+// New is creates a new random UUID or panics. New is equivalent to
+// the expression
+//
+// uuid.Must(uuid.NewRandom())
+func New() UUID {
+ return Must(NewRandom())
+}
+
+// NewRandom returns a Random (Version 4) UUID or panics.
//
// The strength of the UUIDs is based on the strength of the crypto/rand
// package.
@@ -16,10 +26,13 @@
// means the probability is about 0.00000000006 (6 × 10−11),
// equivalent to the odds of creating a few tens of trillions of UUIDs in a
// year and having one duplicate.
-func New() UUID {
+func NewRandom() (UUID, error) {
var uuid UUID
- randomBits([]byte(uuid[:]))
+ _, err := io.ReadFull(rander, uuid[:])
+ if err != nil {
+ return UUID{}, err
+ }
uuid[6] = (uuid[6] & 0x0f) | 0x40 // Version 4
uuid[8] = (uuid[8] & 0x3f) | 0x80 // Variant is 10
- return uuid
+ return uuid, nil
}