Skip to content

Commit bc7f11c

Browse files
[4.2][stdlib] Update default generator to SystemRandomNumberGenerator (#17990)
* Rename SystemRandomNumberGenerator, remove .default
1 parent 00639e3 commit bc7f11c

File tree

7 files changed

+37
-30
lines changed

7 files changed

+37
-30
lines changed

stdlib/public/core/Bool.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,16 @@ public struct Bool {
124124
/// print("Maybe another try?")
125125
/// }
126126
///
127-
/// `Bool.random()` uses the default random generator, `Random.default`. The
128-
/// call in the example above is equivalent to
129-
/// `Bool.random(using: &Random.default)`.
127+
/// `Bool.random()` uses the default random generator,
128+
/// `SystemRandomNumberGenerator`. To supply a non-default generator, call the
129+
/// equivalent method that takes one as an argument.
130130
///
131131
/// - Returns: Either `true` or `false`, randomly chosen with equal
132132
/// probability.
133133
@inlinable
134134
public static func random() -> Bool {
135-
return Bool.random(using: &Random.default)
135+
var g = SystemRandomNumberGenerator()
136+
return Bool.random(using: &g)
136137
}
137138
}
138139

stdlib/public/core/Collection.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,8 @@ extension Collection {
10611061
/// empty, the method returns `nil`.
10621062
@inlinable
10631063
public func randomElement() -> Element? {
1064-
return randomElement(using: &Random.default)
1064+
var g = SystemRandomNumberGenerator()
1065+
return randomElement(using: &g)
10651066
}
10661067

10671068
/// Do not use this method directly; call advanced(by: n) instead.

stdlib/public/core/CollectionAlgorithms.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,8 @@ extension Sequence {
416416
/// - Complexity: O(*n*)
417417
@inlinable
418418
public func shuffled() -> [Element] {
419-
return shuffled(using: &Random.default)
419+
var g = SystemRandomNumberGenerator()
420+
return shuffled(using: &g)
420421
}
421422
}
422423

@@ -471,7 +472,8 @@ extension MutableCollection where Self : RandomAccessCollection {
471472
/// - Complexity: O(*n*)
472473
@inlinable
473474
public mutating func shuffle() {
474-
shuffle(using: &Random.default)
475+
var g = SystemRandomNumberGenerator()
476+
shuffle(using: &g)
475477
}
476478
}
477479

stdlib/public/core/FloatingPoint.swift.gyb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2515,7 +2515,8 @@ where Self.RawSignificand : FixedWidthInteger {
25152515
/// - Returns: A random value within the bounds of `range`.
25162516
@inlinable
25172517
public static func random(in range: ${Range}<Self>) -> Self {
2518-
return Self.random(in: range, using: &Random.default)
2518+
var g = SystemRandomNumberGenerator()
2519+
return Self.random(in: range, using: &g)
25192520
}
25202521
}
25212522

stdlib/public/core/Integers.swift.gyb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2629,7 +2629,8 @@ extension FixedWidthInteger {
26292629
/// - Returns: A random value within the bounds of `range`.
26302630
@inlinable
26312631
public static func random(in range: ${Range}<Self>) -> Self {
2632-
return Self.random(in: range, using: &Random.default)
2632+
var g = SystemRandomNumberGenerator()
2633+
return Self.random(in: range, using: &g)
26332634
}
26342635
}
26352636

stdlib/public/core/Random.swift

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import SwiftShims
3232
/// }
3333
///
3434
/// static func random() -> Weekday {
35-
/// return Weekday.randomWeekday(using: &Random.default)
35+
/// return Weekday.random(using: &Random.default)
3636
/// }
3737
/// }
3838
///
@@ -60,6 +60,7 @@ public protocol RandomNumberGenerator {
6060
}
6161

6262
extension RandomNumberGenerator {
63+
@inlinable
6364
public mutating func _fill(bytes buffer: UnsafeMutableRawBufferPointer) {
6465
// FIXME: Optimize
6566
var chunk: UInt64 = 0
@@ -112,46 +113,45 @@ extension RandomNumberGenerator {
112113
/// The default source of random data.
113114
///
114115
/// When you generate random values, shuffle a collection, or perform another
115-
/// operation that depends on random data, this type's `default` property is
116-
/// the generator used by default. For example, the two method calls in this
117-
/// example are equivalent:
116+
/// operation that depends on random data, this type's `default` property is the
117+
/// generator used by default. For example, the two method calls in this example
118+
/// are equivalent:
118119
///
119120
/// let x = Int.random(in: 1...100)
120-
/// let y = Int.random(in: 1...100, using: &Random.default)
121+
/// var g = SystemRandomNumberGenerator()
122+
/// let y = Int.random(in: 1...100, using: &g)
121123
///
122-
/// `Random.default` is automatically seeded, is safe to use in multiple
123-
/// threads, and uses a cryptographically secure algorithm whenever possible.
124+
/// `SystemRandomNumberGenerator` is automatically seeded, is safe to use in
125+
/// multiple threads, and uses a cryptographically secure algorithm whenever
126+
/// possible.
124127
///
125128
/// Platform Implementation of `Random`
126129
/// ===================================
127130
///
128-
/// While the `Random.default` generator is automatically seeded and
129-
/// thread-safe on every platform, the cryptographic quality of the stream of
130-
/// random data produced by the generator may vary. For more detail, see the
131+
/// While the `SystemRandomNumberGenerator` generator is automatically seeded
132+
/// and thread-safe on every platform, the cryptographic quality of the stream
133+
/// of random data produced by the generator may vary. For more detail, see the
131134
/// documentation for the APIs used by each platform.
132135
///
133136
/// - Apple platforms use `arc4random_buf(3)`.
134137
/// - Linux platforms use `getrandom(2)` when available; otherwise, they read
135138
/// from `/dev/urandom`.
136-
public struct Random : RandomNumberGenerator {
137-
/// The default instance of the `Random` random number generator.
138-
public static var `default`: Random {
139-
get { return Random() }
140-
set { /* Discard */ }
141-
}
142-
143-
private init() {}
139+
@_fixed_layout
140+
public struct SystemRandomNumberGenerator : RandomNumberGenerator {
141+
@inlinable
142+
public init() { }
144143

145144
/// Returns a value from a uniform, independent distribution of binary data.
146145
///
147146
/// - Returns: An unsigned 64-bit random value.
148-
@effects(releasenone)
147+
@inlinable
149148
public mutating func next() -> UInt64 {
150149
var random: UInt64 = 0
151150
_stdlib_random(&random, MemoryLayout<UInt64>.size)
152151
return random
153152
}
154153

154+
@inlinable
155155
public mutating func _fill(bytes buffer: UnsafeMutableRawBufferPointer) {
156156
if !buffer.isEmpty {
157157
_stdlib_random(buffer.baseAddress!, buffer.count)

validation-test/stdlib/Random.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ RandomTests.test("_fill(bytes:)") {
1717
expectEqual(bytes1, zeros)
1818
expectEqual(bytes2, zeros)
1919

20-
bytes1.withUnsafeMutableBytes { Random.default._fill(bytes: $0) }
20+
var g = SystemRandomNumberGenerator()
21+
bytes1.withUnsafeMutableBytes { g._fill(bytes: $0) }
2122
expectNotEqual(bytes1, bytes2)
2223
expectNotEqual(bytes1, zeros)
2324

24-
bytes2.withUnsafeMutableBytes { Random.default._fill(bytes: $0) }
25+
bytes2.withUnsafeMutableBytes { g._fill(bytes: $0) }
2526
expectNotEqual(bytes1, bytes2)
2627
expectNotEqual(bytes2, zeros)
2728
}

0 commit comments

Comments
 (0)