@@ -6,13 +6,25 @@ import Accelerate
66
77@_documentation ( visibility: private)
88public protocol Random {
9- static func random( size: Int , seed: UInt64 ? ) -> Vector < Self >
9+ static func random( size: Int ) -> Vector < Self >
10+ static func random( size: Int , seed: UInt64 ) -> Vector < Self >
11+ static func random< G: RandomNumberGenerator > ( size: Int , rng: inout G ) -> Vector < Self >
1012 static func randomBNNS( size: Int , bounds: ( Self , Self ) , seed: UInt64 , buffer: UnsafeMutableBufferPointer < Self > )
1113}
1214
1315@_documentation ( visibility: private)
1416extension Float : Random {
15- public static func random( size: Int , seed: UInt64 ? ) -> Vector < Float > {
17+
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 > {
1628 var vec = Vector < Float > ( size: size)
1729 var rng = Wyrand ( seed: seed)
1830 for i in 0 ..< size {
@@ -21,6 +33,14 @@ extension Float: Random {
2133 return vec
2234 }
2335
36+ public static func random< G: RandomNumberGenerator > ( size: Int , rng: inout G ) -> Vector < Float > {
37+ var vec = Vector < Float > ( size: size)
38+ for i in 0 ..< size {
39+ vec [ i] = Float ( rng. next ( ) >> 40 ) * 0x1 . 0 p- 24
40+ }
41+ return vec
42+ }
43+
2444 public static func randomBNNS(
2545 size: Int , bounds: ( Float , Float ) ,
2646 seed: UInt64 , buffer: UnsafeMutableBufferPointer < Float >
@@ -33,7 +53,17 @@ extension Float: Random {
3353
3454@_documentation ( visibility: private)
3555extension Double : Random {
36- public static func random( size: Int , seed: UInt64 ? ) -> Vector < Double > {
56+
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 > {
3767 var vec = Vector < Double > ( size: size)
3868 var rng = Wyrand ( seed: seed)
3969 for i in 0 ..< size {
@@ -42,6 +72,14 @@ extension Double: Random {
4272 return vec
4373 }
4474
75+ public static func random< G: RandomNumberGenerator > ( size: Int , rng: inout G ) -> Vector < Double > {
76+ var vec = Vector < Double > ( size: size)
77+ for i in 0 ..< size {
78+ vec [ i] = Double ( rng. next ( ) >> 11 ) * 0x1 . 0 p- 53
79+ }
80+ return vec
81+ }
82+
4583 public static func randomBNNS(
4684 size: Int , bounds: ( Double , Double ) ,
4785 seed: UInt64 , buffer: UnsafeMutableBufferPointer < Double >
@@ -52,21 +90,49 @@ extension Double: Random {
5290
5391extension Vector where Scalar: Random {
5492
55- /// Create a vector of random values from a uniform distrubtion over [0, 1).
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.
56106 /// ```swift
57- /// let vec = Vector<Double>.random(size: 5 )
107+ /// let vec = Vector<Double>.random(5, seed: 123456 )
58108 /// ```
59- /// This uses the Wyrand pseudorandom number generator (PRNG) to generate the random values in the vector.
60- /// Values are a uniform distribution over 0 to 1 excluding 1 which is denoted as [0, 1).
61- /// Provide a seed input value to create a reproducible random vector .
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 .
62112 /// - Parameters:
63113 /// - size: Size of the vector.
64- /// - seed: Seed for the random number generator.
114+ /// - seed: Seed for the Wyrand generator.
65115 /// - Returns: Vector of random values.
66- public static func random( size: Int , seed: UInt64 ? = nil ) -> Vector {
116+ public static func random( _ size: Int , seed: UInt64 ) -> Vector {
67117 Scalar . random ( size: size, seed: seed)
68118 }
69119
120+ /// Create a vector of random values from a uniform distribution over [0, 1) using a random number generator.
121+ /// ```swift
122+ /// var rng = Xoroshiro128Plus(seed: (1227493545477, 7213431619994))
123+ /// let a = Vector<Float>.random(5, using: &rng)
124+ /// ```
125+ /// 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.
128+ /// - Parameters:
129+ /// - size: Size of the vector.
130+ /// - rng: Random number generator.
131+ /// - Returns: Vector or random values.
132+ public static func random< G: RandomNumberGenerator > ( _ size: Int , using rng: inout G ) -> Vector {
133+ Scalar . random ( size: size, rng: & rng)
134+ }
135+
70136 /// Create a vector of random float values using BNNS.
71137 /// - Parameters:
72138 /// - size: Size of the vector.
0 commit comments