Skip to content

Commit 2f4294b

Browse files
committed
Value type generators
random -> randomElement Fix some docs One more doc fix Doc fixes & bool fix Use computed over explicit
1 parent f31c344 commit 2f4294b

File tree

9 files changed

+80
-73
lines changed

9 files changed

+80
-73
lines changed

benchmark/single-source/RandomShuffle.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ public let RandomShuffle = [
2525
]
2626

2727
/// A linear congruential PRNG.
28-
final class LCRNG: RandomNumberGenerator {
28+
struct LCRNG: RandomNumberGenerator {
2929
private var state: UInt64
3030

3131
init(seed: Int) {
3232
state = UInt64(truncatingIfNeeded: seed)
3333
for _ in 0..<10 { _ = next() }
3434
}
3535

36-
func next() -> UInt64 {
36+
mutating func next() -> UInt64 {
3737
state = 2862933555777941757 &* state &+ 3037000493
3838
return state
3939
}
@@ -56,9 +56,9 @@ public func run_RandomShuffleDef(_ N: Int) {
5656

5757
@inline(never)
5858
public func run_RandomShuffleLCG(_ N: Int) {
59-
let generator = LCRNG(seed: 0)
59+
var generator = LCRNG(seed: 0)
6060
for _ in 0 ..< N {
61-
numbers.shuffle(using: generator)
61+
numbers.shuffle(using: &generator)
6262
blackHole(numbers.first!)
6363
}
6464
}

benchmark/single-source/RandomValues.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ public let RandomValues = [
2626
]
2727

2828
/// A linear congruential PRNG.
29-
final class LCRNG: RandomNumberGenerator {
29+
struct LCRNG: RandomNumberGenerator {
3030
private var state: UInt64
3131

3232
init(seed: Int) {
3333
state = UInt64(truncatingIfNeeded: seed)
3434
for _ in 0..<10 { _ = next() }
3535
}
3636

37-
func next() -> UInt64 {
37+
mutating func next() -> UInt64 {
3838
state = 2862933555777941757 &* state &+ 3037000493
3939
return state
4040
}
@@ -55,9 +55,9 @@ public func run_RandomIntegersDef(_ N: Int) {
5555
public func run_RandomIntegersLCG(_ N: Int) {
5656
for _ in 0 ..< N {
5757
var x = 0
58-
let generator = LCRNG(seed: 0)
58+
var generator = LCRNG(seed: 0)
5959
for _ in 0 ..< 100_000 {
60-
x &+= Int.random(in: 0...10_000, using: generator)
60+
x &+= Int.random(in: 0...10_000, using: &generator)
6161
}
6262
CheckResults(x == 498214315)
6363
}
@@ -78,9 +78,9 @@ public func run_RandomDoubleDef(_ N: Int) {
7878
public func run_RandomDoubleLCG(_ N: Int) {
7979
for _ in 0 ..< N {
8080
var x = 0.0
81-
let generator = LCRNG(seed: 0)
81+
var generator = LCRNG(seed: 0)
8282
for _ in 0 ..< 100_000 {
83-
x += Double.random(in: -1000...1000, using: generator)
83+
x += Double.random(in: -1000...1000, using: &generator)
8484
}
8585
blackHole(x)
8686
}

stdlib/public/core/Bool.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ public struct Bool {
9595
/// - Returns: A random Boolean.
9696
@inlinable
9797
public static func random<T: RandomNumberGenerator>(
98-
using generator: T
98+
using generator: inout T
9999
) -> Bool {
100-
return generator.next() % 2 == 0
100+
return (generator.next() >> 17) & 1 == 0
101101
}
102102

103103
/// Returns a random Boolean
@@ -109,7 +109,7 @@ public struct Bool {
109109
/// This uses the standard library's default random number generator.
110110
@inlinable
111111
public static func random() -> Bool {
112-
return Bool.random(using: Random.default)
112+
return Bool.random(using: &Random.default)
113113
}
114114
}
115115

stdlib/public/core/Collection.swift

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -795,17 +795,19 @@ public protocol Collection: Sequence where SubSequence: Collection {
795795
///
796796
/// You use this method to select a random element from a collection when you
797797
/// are using a custom random number generator. For example, call
798-
/// `random(using:)` to select a random element from an array of names.
798+
/// `randomElement(using:)` to select a random element from an array of names.
799799
///
800800
/// let names = ["Zoey", "Chloe", "Amani", "Amaia"]
801-
/// let randomName = names.random(using: myGenerator)!
801+
/// let randomName = names.randomElement(using: &myGenerator)!
802802
/// // randomName == "Amani" (maybe)
803803
///
804804
/// - Parameter generator: The random number generator to use when choosing
805805
/// a random element.
806806
/// - Returns: A random element from the collection. If the collection is
807807
/// empty, the method returns `nil`.
808-
func random<T: RandomNumberGenerator>(using generator: T) -> Element?
808+
func randomElement<T: RandomNumberGenerator>(
809+
using generator: inout T
810+
) -> Element?
809811

810812
@available(*, deprecated, message: "all index distances are now of type Int")
811813
typealias IndexDistance = Int
@@ -1025,19 +1027,19 @@ extension Collection {
10251027
///
10261028
/// You use this method to select a random element from a collection when you
10271029
/// are using a custom random number generator. For example, call
1028-
/// `random(using:)` to select a random element from an array of names.
1030+
/// `randomElement(using:)` to select a random element from an array of names.
10291031
///
10301032
/// let names = ["Zoey", "Chloe", "Amani", "Amaia"]
1031-
/// let randomName = names.random(using: myGenerator)!
1033+
/// let randomName = names.randomElement(using: &myGenerator)!
10321034
/// // randomName == "Amani" (maybe)
10331035
///
10341036
/// - Parameter generator: The random number generator to use when choosing
10351037
/// a random element.
10361038
/// - Returns: A random element from the collection. If the collection is
10371039
/// empty, the method returns `nil`.
10381040
@inlinable
1039-
public func random<T: RandomNumberGenerator>(
1040-
using generator: T
1041+
public func randomElement<T: RandomNumberGenerator>(
1042+
using generator: inout T
10411043
) -> Element? {
10421044
guard !isEmpty else { return nil }
10431045
let random = generator.next(upperBound: UInt(count))
@@ -1050,22 +1052,22 @@ extension Collection {
10501052

10511053
/// Returns a random element of the collection.
10521054
///
1053-
/// For example, call `random()` to select a random element from an
1055+
/// For example, call `randomElement()` to select a random element from an
10541056
/// array of names.
10551057
///
10561058
/// let names = ["Zoey", "Chloe", "Amani", "Amaia"]
1057-
/// let randomName = names.random()!
1059+
/// let randomName = names.randomElement()!
10581060
/// // randomName == "Amani" (perhaps)
10591061
///
10601062
/// This method uses the default random generator, `Random.default`. The call
1061-
/// to `names.random()` above is equivalent to calling
1062-
/// `names.random(using: Random.default)`.
1063+
/// to `names.randomElement()` above is equivalent to calling
1064+
/// `names.randomElement(using: &Random.default)`.
10631065
///
10641066
/// - Returns: A random element from the collection. If the collection is
10651067
/// empty, the method returns `nil`.
10661068
@inlinable
1067-
public func random() -> Element? {
1068-
return random(using: Random.default)
1069+
public func randomElement() -> Element? {
1070+
return randomElement(using: &Random.default)
10691071
}
10701072

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

stdlib/public/core/CollectionAlgorithms.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ extension Sequence {
274274
/// on that range:
275275
///
276276
/// let numbers = 0...9
277-
/// let shuffledNumbers = numbers.shuffled(using: myGenerator)
277+
/// let shuffledNumbers = numbers.shuffled(using: &myGenerator)
278278
/// // shuffledNumbers == [8, 9, 4, 3, 2, 6, 7, 0, 5, 1]
279279
///
280280
/// - Parameter generator: The random number generator to use when shuffling
@@ -284,10 +284,10 @@ extension Sequence {
284284
/// - Complexity: O(*n*)
285285
@inlinable
286286
public func shuffled<T: RandomNumberGenerator>(
287-
using generator: T
287+
using generator: inout T
288288
) -> [Element] {
289289
var result = ContiguousArray(self)
290-
result.shuffle(using: generator)
290+
result.shuffle(using: &generator)
291291
return Array(result)
292292
}
293293

@@ -302,7 +302,7 @@ extension Sequence {
302302
///
303303
/// This method uses the default random generator, `Random.default`. The call
304304
/// to `numbers.shuffled()` above is equivalent to calling
305-
/// `numbers.shuffled(using: Random.default)`.
305+
/// `numbers.shuffled(using: &Random.default)`.
306306
///
307307
/// - Parameter generator: The random number generator to use when shuffling
308308
/// the sequence.
@@ -311,7 +311,7 @@ extension Sequence {
311311
/// - Complexity: O(*n*)
312312
@inlinable
313313
public func shuffled() -> [Element] {
314-
return shuffled(using: Random.default)
314+
return shuffled(using: &Random.default)
315315
}
316316
}
317317

@@ -324,7 +324,7 @@ extension MutableCollection {
324324
/// `shuffle(using:)` method to randomly reorder the elements of an array.
325325
///
326326
/// var names = ["Alejandro", "Camila", "Diego", "Luciana", "Luis", "Sofía"]
327-
/// names.shuffle(using: myGenerator)
327+
/// names.shuffle(using: &myGenerator)
328328
/// // names == ["Sofía", "Alejandro", "Camila", "Luis", "Diego", "Luciana"]
329329
///
330330
/// - Parameter generator: The random number generator to use when shuffling
@@ -333,7 +333,7 @@ extension MutableCollection {
333333
/// - Complexity: O(*n*)
334334
@inlinable
335335
public mutating func shuffle<T: RandomNumberGenerator>(
336-
using generator: T
336+
using generator: inout T
337337
) {
338338
guard count > 1 else { return }
339339
var amount = count
@@ -360,12 +360,12 @@ extension MutableCollection {
360360
///
361361
/// This method uses the default random generator, `Random.default`. The call
362362
/// to `names.shuffle()` above is equivalent to calling
363-
/// `names.shuffle(using: Random.default)`.
363+
/// `names.shuffle(using: &Random.default)`.
364364
///
365365
/// - Complexity: O(*n*)
366366
@inlinable
367367
public mutating func shuffle() {
368-
shuffle(using: Random.default)
368+
shuffle(using: &Random.default)
369369
}
370370
}
371371

stdlib/public/core/FloatingPoint.swift.gyb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2389,7 +2389,7 @@ where Self.RawSignificand : FixedWidthInteger,
23892389
/// creates three new values in the range `${exampleRange}`.
23902390
///
23912391
/// for _ in 1...3 {
2392-
/// print(Double.random(in: ${exampleRange}, using: myGenerator))
2392+
/// print(Double.random(in: ${exampleRange}, using: &myGenerator))
23932393
/// }
23942394
/// // Prints "18.1900709259179"
23952395
/// // Prints "14.2286325689993"
@@ -2412,7 +2412,7 @@ where Self.RawSignificand : FixedWidthInteger,
24122412
@inlinable
24132413
public static func random<T: RandomNumberGenerator>(
24142414
in range: ${Range}<Self>,
2415-
using generator: T
2415+
using generator: inout T
24162416
) -> Self {
24172417
% if 'Closed' not in Range:
24182418
_precondition(
@@ -2466,7 +2466,7 @@ where Self.RawSignificand : FixedWidthInteger,
24662466
///
24672467
/// This method uses the default random generator, `Random.default`. The call
24682468
/// to `Double.random(in: ${exampleRange})` above is equivalent to calling
2469-
/// `Double.random(in: ${exampleRange}, using: Random.default)`.
2469+
/// `Double.random(in: ${exampleRange}, using: &Random.default)`.
24702470
///
24712471
/// - Parameter range: The range in which to create a random value.
24722472
% if Range == 'Range':
@@ -2475,7 +2475,7 @@ where Self.RawSignificand : FixedWidthInteger,
24752475
/// - Returns: A random value within the bounds of `range`.
24762476
@inlinable
24772477
public static func random(in range: ${Range}<Self>) -> Self {
2478-
return Self.random(in: range, using: Random.default)
2478+
return Self.random(in: range, using: &Random.default)
24792479
}
24802480
}
24812481

stdlib/public/core/Integers.swift.gyb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2510,8 +2510,8 @@ extension ${Range}
25102510
/// This method never returns `nil`.
25112511
% end
25122512
@inlinable
2513-
public func random<T: RandomNumberGenerator>(
2514-
using generator: T
2513+
public func randomElement<T: RandomNumberGenerator>(
2514+
using generator: inout T
25152515
) -> Element? {
25162516
% if 'Closed' not in Range:
25172517
guard lowerBound != upperBound else {
@@ -2565,8 +2565,8 @@ extension ${Range}
25652565
/// value.
25662566
///
25672567
/// This method uses the default random generator, `Random.default`. Calling
2568-
/// `(${exampleRange}).random()` is equivalent to calling
2569-
/// `(${exampleRange}).random(using: Random.default)`.
2568+
/// `(${exampleRange}).randomElement()` is equivalent to calling
2569+
/// `(${exampleRange}).randomElement(using: &Random.default)`.
25702570
///
25712571
/// - Returns: A random element of the range.
25722572
% if 'Closed' not in Range:
@@ -2575,8 +2575,8 @@ extension ${Range}
25752575
/// This method never returns `nil`.
25762576
% end
25772577
@inlinable
2578-
public func random() -> Element? {
2579-
return self.random(using: Random.default)
2578+
public func randomElement() -> Element? {
2579+
return randomElement(using: &Random.default)
25802580
}
25812581
}
25822582
@@ -2592,7 +2592,7 @@ where Self.Stride : SignedInteger,
25922592
/// new values in the range `${exampleRange}`.
25932593
///
25942594
/// for _ in 1...3 {
2595-
/// print(Int.random(in: ${exampleRange}, using: myGenerator))
2595+
/// print(Int.random(in: ${exampleRange}, using: &myGenerator))
25962596
/// }
25972597
/// // Prints "7"
25982598
/// // Prints "44"
@@ -2609,15 +2609,15 @@ where Self.Stride : SignedInteger,
26092609
@inlinable
26102610
public static func random<T: RandomNumberGenerator>(
26112611
in range: ${Range}<Self>,
2612-
using generator: T
2612+
using generator: inout T
26132613
) -> Self {
26142614
% if 'Closed' not in Range:
26152615
_precondition(
26162616
range.lowerBound != range.upperBound,
26172617
"Can't get random value with lowerBound == upperBound"
26182618
)
26192619
% end
2620-
return range.random(using: generator)!
2620+
return range.randomElement(using: &generator)!
26212621
}
26222622
26232623
/// Returns a random value within the specified range.
@@ -2634,7 +2634,7 @@ where Self.Stride : SignedInteger,
26342634
///
26352635
/// This method uses the default random generator, `Random.default`. The call
26362636
/// to `Int.random(in: ${exampleRange})` above is equivalent to calling
2637-
/// `Int.random(in: ${exampleRange}, using: Random.default)`.
2637+
/// `Int.random(in: ${exampleRange}, using: &Random.default)`.
26382638
///
26392639
/// - Parameter range: The range in which to create a random value.
26402640
% if Range == 'Range':
@@ -2643,7 +2643,7 @@ where Self.Stride : SignedInteger,
26432643
/// - Returns: A random value within the bounds of `range`.
26442644
@inlinable
26452645
public static func random(in range: ${Range}<Self>) -> Self {
2646-
return Self.random(in: range, using: Random.default)
2646+
return Self.random(in: range, using: &Random.default)
26472647
}
26482648
}
26492649

0 commit comments

Comments
 (0)