@@ -77,16 +77,17 @@ Swift 4.2
77
77
* [ SE-0202] [ ]
78
78
79
79
The standard library now provides a unified set of randomization functionality.
80
- Integer types, floating point types, and Bool all introduce a new random
81
- function .
80
+ Integer types, floating point types, and Bool all introduce a new static
81
+ method that creates a random value .
82
82
83
83
``` swift
84
84
let diceRoll = Int .random (in : 1 ... 6 )
85
85
let randomUnit = Double .random (in : 0 ..< 1 )
86
86
let randomBool = Bool .random ()
87
87
```
88
88
89
- There are also a couple of collection additions included as well.
89
+ There are also additions to select a random element from a collection or
90
+ shuffle its contents.
90
91
91
92
``` swift
92
93
let greetings = [" hey" , " hello" , " hi" , " hola" ]
@@ -96,27 +97,20 @@ Swift 4.2
96
97
97
98
Core to the randomization functionality is a new ` RandomNumberGenerator `
98
99
protocol. The standard library defines its own random number generator
99
- called ` SystemRandomNumberGenerator ` which is backed by a secure and thread
100
- safe random number generator on each platform. All the randomization functions
101
- have a ` using: ` parameter that take a ` RandomNumberGenerator ` that users can
102
- pass in their own random number generator.
100
+ called ` SystemRandomNumberGenerator ` which is backed by a secure and
101
+ thread- safe random number generator on each platform. All the randomization
102
+ functions have a ` using: ` parameter that take a ` RandomNumberGenerator ` that
103
+ users can pass in their own random number generator.
103
104
104
105
``` swift
105
106
struct MersenneTwister : RandomNumberGenerator {
106
107
func next () -> UInt64 {
107
- // Algorithm stuff here
108
+ // implementation
108
109
}
109
110
}
110
111
111
112
var mt = MersenneTwister ()
112
113
let diceRoll = Int .random (in : 1 ... 6 , using : & mt)
113
- let randomUnit = Double .random (in : 0 ..< 1 , using : & mt)
114
- let randomBool = Bool .random (using : & mt)
115
-
116
- // Collection additions
117
- let greetings = [" hey" , " hello" , " hi" , " hola" ]
118
- let randomGreeting = greetings.randomElement (using : & mt)! // This returns an Optional
119
- let newGreetings = greetings.shuffled (using : & mt) // ["hola", "hi", "hey", "hello"]
120
114
```
121
115
122
116
* [ SE-0194] [ ]
0 commit comments