@@ -128,6 +128,45 @@ Swift 4.2
128
128
129
129
### 2018-09-17 (Xcode 10.0)
130
130
131
+ * [ SE-0202] [ ]
132
+
133
+ The standard library now provides a unified set of randomization functionality.
134
+ Integer types, floating point types, and Bool all introduce a new static
135
+ method that creates a random value.
136
+
137
+ ``` swift
138
+ let diceRoll = Int .random (in : 1 ... 6 )
139
+ let randomUnit = Double .random (in : 0 ..< 1 )
140
+ let randomBool = Bool .random ()
141
+ ```
142
+
143
+ There are also additions to select a random element from a collection or
144
+ shuffle its contents.
145
+
146
+ ``` swift
147
+ let greetings = [" hey" , " hello" , " hi" , " hola" ]
148
+ let randomGreeting = greetings.randomElement ()! // This returns an Optional
149
+ let newGreetings = greetings.shuffled () // ["hola", "hi", "hey", "hello"]
150
+ ```
151
+
152
+ Core to the randomization functionality is a new ` RandomNumberGenerator `
153
+ protocol. The standard library defines its own random number generator
154
+ called ` SystemRandomNumberGenerator ` which is backed by a secure and
155
+ thread-safe random number generator on each platform. All the randomization
156
+ functions have a ` using: ` parameter that take a ` RandomNumberGenerator ` that
157
+ users can pass in their own random number generator.
158
+
159
+ ``` swift
160
+ struct MersenneTwister : RandomNumberGenerator {
161
+ func next () -> UInt64 {
162
+ // implementation
163
+ }
164
+ }
165
+
166
+ var mt = MersenneTwister ()
167
+ let diceRoll = Int .random (in : 1 ... 6 , using : & mt)
168
+ ```
169
+
131
170
* [ SE-0194] [ ]
132
171
133
172
The new CaseIterable protocol describes types which have a static
0 commit comments