11/*
2- Random vector protocol using WyRand.
2+ Random protocol.
3+ Random extensions for Float and Double.
4+ Random extensions for Vector.
35*/
46
57import Accelerate
68
79@_documentation ( visibility: private)
810public protocol Random {
9- static func random( size: Int ) -> Vector < Self >
10- static func random( size: Int , seed: UInt64 ) -> Vector < Self >
11+ static func random( size: Int , seed: UInt64 ? ) -> Vector < Self >
1112 static func random< G: RandomNumberGenerator > ( size: Int , rng: inout G ) -> Vector < Self >
1213 static func randomBNNS( size: Int , bounds: ( Self , Self ) , seed: UInt64 , buffer: UnsafeMutableBufferPointer < Self > )
1314}
1415
1516@_documentation ( visibility: private)
1617extension Float : Random {
1718
18- public static func random( size: Int ) -> Vector < Float > {
19- var vec = Vector < Float > ( size: size)
20- var rng = Wyrand ( )
21- for i in 0 ..< size {
22- vec [ i] = rng. next ( )
23- }
24- return vec
25- }
26-
27- public static func random( size: Int , seed: UInt64 ) -> Vector < Float > {
19+ public static func random( size: Int , seed: UInt64 ? ) -> Vector < Float > {
2820 var vec = Vector < Float > ( size: size)
2921 var rng = Wyrand ( seed: seed)
3022 for i in 0 ..< size {
@@ -54,16 +46,7 @@ extension Float: Random {
5446@_documentation ( visibility: private)
5547extension Double : Random {
5648
57- public static func random( size: Int ) -> Vector < Double > {
58- var vec = Vector < Double > ( size: size)
59- var rng = Wyrand ( )
60- for i in 0 ..< size {
61- vec [ i] = rng. next ( )
62- }
63- return vec
64- }
65-
66- public static func random( size: Int , seed: UInt64 ) -> Vector < Double > {
49+ public static func random( size: Int , seed: UInt64 ? ) -> Vector < Double > {
6750 var vec = Vector < Double > ( size: size)
6851 var rng = Wyrand ( seed: seed)
6952 for i in 0 ..< size {
@@ -90,30 +73,19 @@ extension Double: Random {
9073
9174extension Vector where Scalar: Random {
9275
93- /// Create a vector of random values from a uniform distribution over [0, 1) using the Wyrand generator.
94- /// ```swift
95- /// let vec = Vector<Double>.random(5)
96- /// ```
97- /// This uses the ``Wyrand`` pseudorandom number generator (PRNG) to generate the random values in the vector.
98- /// The values are sampled from a uniform distribution [0, 1) which includes zero but excludes one.
99- /// - Parameter size: Size of the vector.
100- /// - Returns: Vector of random values.
101- public static func random( _ size: Int ) -> Vector {
102- Scalar . random ( size: size)
103- }
104-
105- /// Create a vector of random values from a uniform distribution over [0, 1) using a seed for the Wyrand generator.
76+ /// Create a vector of random values from a uniform distribution over [0, 1).
10677 /// ```swift
107- /// let vec = Vector<Double>.random(5, seed: 123456)
78+ /// let a = Vector<Float>.random(5)
79+ /// let b = Vector<Double>.random(8, seed: 123456)
10880 /// ```
109- /// This uses the ``Wyrand`` pseudorandom number generator (PRNG) to generate the random values in the vector.
110- /// The values are sampled from a uniform distribution [0, 1) which includes zero but excludes one. Provide a seed
111- /// for reproducible results.
81+ /// This method uses the ``Wyrand`` pseudorandom number generator (PRNG) to generate the random values in the
82+ /// vector. The values are sampled from a uniform distribution [0, 1) which includes zero but excludes one. An
83+ /// optional seed can be provided for reproducible results.
11284 /// - Parameters:
11385 /// - size: Size of the vector.
11486 /// - seed: Seed for the Wyrand generator.
11587 /// - Returns: Vector of random values.
116- public static func random( _ size: Int , seed: UInt64 ) -> Vector {
88+ public static func random( _ size: Int , seed: UInt64 ? = nil ) -> Vector {
11789 Scalar . random ( size: size, seed: seed)
11890 }
11991
@@ -123,8 +95,7 @@ extension Vector where Scalar: Random {
12395 /// let a = Vector<Float>.random(5, using: &rng)
12496 /// ```
12597 /// The example uses the ``Xoroshiro128Plus`` pseudorandom number generator (PRNG) to generate random values to
126- /// fill the vector. The generator is seeded with two integers otherwise random seeds are provided if none are
127- /// given.
98+ /// fill the vector. The generator is seeded with two integers otherwise random seeds are used if none are given.
12899 /// - Parameters:
129100 /// - size: Size of the vector.
130101 /// - rng: Random number generator.
@@ -140,7 +111,7 @@ extension Vector where Scalar: Random {
140111 /// - seed: Seed for the random number generator.
141112 /// - Returns: Vector of random float values.
142113 public static func randomBNNS( size: Int , bounds: ( Scalar , Scalar ) , seed: UInt64 ? = nil ) -> Vector {
143- let s = seed ?? UInt64 ( abs ( UUID ( ) . hashValue ) )
114+ let s = seed ?? UInt64 . random ( in : 0 ..< UInt64 . max )
144115 let vec = Vector < Scalar > ( size: size)
145116 Scalar . randomBNNS ( size: size, bounds: bounds, seed: s, buffer: vec. buffer)
146117 return vec
0 commit comments