Skip to content

Commit 9ccabe4

Browse files
authored
Merge pull request swiftlang#19383 from Azoy/se-0202-changelog
2 parents 30d1568 + eb85cba commit 9ccabe4

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

CHANGELOG.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,45 @@ Swift 4.2
128128

129129
### 2018-09-17 (Xcode 10.0)
130130

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+
131170
* [SE-0194][]
132171

133172
The new CaseIterable protocol describes types which have a static

0 commit comments

Comments
 (0)