Skip to content

Commit 161819b

Browse files
authored
Merge pull request swiftlang#18634 from natecook1000/nc-random-highlevel-notes
2 parents c1f25e0 + 908ac9f commit 161819b

File tree

5 files changed

+59
-28
lines changed

5 files changed

+59
-28
lines changed

stdlib/public/core/Bool.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ public struct Bool {
100100
/// print("Maybe another try?")
101101
/// }
102102
///
103+
/// - Note: The algorithm used to create random values may change in a future
104+
/// version of Swift. If you're passing a generator that results in the
105+
/// same sequence of Boolean values each time you run your program, that
106+
/// sequence may change when your program is compiled using a different
107+
/// version of Swift.
108+
///
103109
/// - Parameter generator: The random number generator to use when creating
104110
/// the new random value.
105111
/// - Returns: Either `true` or `false`, randomly chosen with equal
@@ -122,9 +128,8 @@ public struct Bool {
122128
/// print("Maybe another try?")
123129
/// }
124130
///
125-
/// `Bool.random()` uses the default random generator,
126-
/// `SystemRandomNumberGenerator`. To supply a non-default generator, call the
127-
/// equivalent method that takes one as an argument.
131+
/// This method is equivalent to calling `Bool.random(using:)`, passing in
132+
/// the system's default random generator.
128133
///
129134
/// - Returns: Either `true` or `false`, randomly chosen with equal
130135
/// probability.

stdlib/public/core/Collection.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,14 +1029,19 @@ extension Collection {
10291029
/// let randomName = names.randomElement(using: &myGenerator)!
10301030
/// // randomName == "Amani"
10311031
///
1032-
/// - Parameter generator: The random number generator to use when choosing
1033-
/// a random element.
1032+
/// - Parameter generator: The random number generator to use when choosing a
1033+
/// random element.
10341034
/// - Returns: A random element from the collection. If the collection is
10351035
/// empty, the method returns `nil`.
10361036
///
10371037
/// - Complexity: O(1) if the collection conforms to
1038-
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the length of
1039-
/// the collection.
1038+
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the length
1039+
/// of the collection.
1040+
/// - Note: The algorithm used to select a random element may change in a
1041+
/// future version of Swift. If you're passing a generator that results in
1042+
/// the same sequence of elements each time you run your program, that
1043+
/// sequence may change when your program is compiled using a different
1044+
/// version of Swift.
10401045
@inlinable
10411046
public func randomElement<T: RandomNumberGenerator>(
10421047
using generator: inout T
@@ -1059,15 +1064,15 @@ extension Collection {
10591064
/// let randomName = names.randomElement()!
10601065
/// // randomName == "Amani"
10611066
///
1062-
/// This method is equivalent to calling the version that takes a generator,
1063-
/// passing in the system's default random generator.
1067+
/// This method is equivalent to calling `randomElement(using:)`, passing in
1068+
/// the system's default random generator.
10641069
///
10651070
/// - Returns: A random element from the collection. If the collection is
10661071
/// empty, the method returns `nil`.
10671072
///
10681073
/// - Complexity: O(1) if the collection conforms to
1069-
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the length of
1070-
/// the collection.
1074+
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the length
1075+
/// of the collection.
10711076
@inlinable
10721077
public func randomElement() -> Element? {
10731078
var g = SystemRandomNumberGenerator()

stdlib/public/core/CollectionAlgorithms.swift

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -370,10 +370,10 @@ extension Sequence {
370370
/// Returns the elements of the sequence, shuffled using the given generator
371371
/// as a source for randomness.
372372
///
373-
/// You use this method to randomize the elements of a sequence when you
374-
/// are using a custom random number generator. For example, you can shuffle
375-
/// the numbers between `0` and `9` by calling the `shuffled(using:)` method
376-
/// on that range:
373+
/// You use this method to randomize the elements of a sequence when you are
374+
/// using a custom random number generator. For example, you can shuffle the
375+
/// numbers between `0` and `9` by calling the `shuffled(using:)` method on
376+
/// that range:
377377
///
378378
/// let numbers = 0...9
379379
/// let shuffledNumbers = numbers.shuffled(using: &myGenerator)
@@ -384,6 +384,11 @@ extension Sequence {
384384
/// - Returns: An array of this sequence's elements in a shuffled order.
385385
///
386386
/// - Complexity: O(*n*), where *n* is the length of the sequence.
387+
/// - Note: The algorithm used to shuffle a sequence may change in a future
388+
/// version of Swift. If you're passing a generator that results in the
389+
/// same shuffled order each time you run your program, that sequence may
390+
/// change when your program is compiled using a different version of
391+
/// Swift.
387392
@inlinable
388393
public func shuffled<T: RandomNumberGenerator>(
389394
using generator: inout T
@@ -402,8 +407,8 @@ extension Sequence {
402407
/// let shuffledNumbers = numbers.shuffled()
403408
/// // shuffledNumbers == [1, 7, 6, 2, 8, 9, 4, 3, 5, 0]
404409
///
405-
/// This method is equivalent to calling the version that takes a generator,
406-
/// passing in the system's default random generator.
410+
/// This method is equivalent to calling `shuffled(using:)`, passing in the
411+
/// system's default random generator.
407412
///
408413
/// - Returns: A shuffled array of this sequence's elements.
409414
///
@@ -431,6 +436,11 @@ extension MutableCollection where Self : RandomAccessCollection {
431436
/// the collection.
432437
///
433438
/// - Complexity: O(*n*), where *n* is the length of the collection.
439+
/// - Note: The algorithm used to shuffle a collection may change in a future
440+
/// version of Swift. If you're passing a generator that results in the
441+
/// same shuffled order each time you run your program, that sequence may
442+
/// change when your program is compiled using a different version of
443+
/// Swift.
434444
@inlinable
435445
public mutating func shuffle<T: RandomNumberGenerator>(
436446
using generator: inout T
@@ -452,15 +462,14 @@ extension MutableCollection where Self : RandomAccessCollection {
452462

453463
/// Shuffles the collection in place.
454464
///
455-
/// Use the `shuffle()` method to randomly reorder the elements of an
456-
/// array.
465+
/// Use the `shuffle()` method to randomly reorder the elements of an array.
457466
///
458467
/// var names = ["Alejandro", "Camila", "Diego", "Luciana", "Luis", "Sofía"]
459468
/// names.shuffle(using: myGenerator)
460469
/// // names == ["Luis", "Camila", "Luciana", "Sofía", "Alejandro", "Diego"]
461470
///
462-
/// This method is equivalent to calling the version that takes a generator,
463-
/// passing in the system's default random generator.
471+
/// This method is equivalent to calling `shuffle(using:)`, passing in the
472+
/// system's default random generator.
464473
///
465474
/// - Complexity: O(*n*), where *n* is the length of the collection.
466475
@inlinable

stdlib/public/core/FloatingPoint.swift.gyb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2404,9 +2404,15 @@ where Self.RawSignificand : FixedWidthInteger {
24042404
///
24052405
/// The `random(in:using:)` static method chooses a random value from a
24062406
/// continuous uniform distribution in `range`, and then converts that value
2407-
/// to the nearest representable value in this type. Depending on the size and
2408-
/// span of `range`, some concrete values may be represented more frequently
2409-
/// than others.
2407+
/// to the nearest representable value in this type. Depending on the size
2408+
/// and span of `range`, some concrete values may be represented more
2409+
/// frequently than others.
2410+
///
2411+
/// - Note: The algorithm used to create random values may change in a future
2412+
/// version of Swift. If you're passing a generator that results in the
2413+
/// same sequence of floating-point values each time you run your program,
2414+
/// that sequence may change when your program is compiled using a
2415+
/// different version of Swift.
24102416
///
24112417
/// - Parameters:
24122418
/// - range: The range in which to create a random value.
@@ -2488,8 +2494,8 @@ where Self.RawSignificand : FixedWidthInteger {
24882494
/// of `range`, some concrete values may be represented more frequently than
24892495
/// others.
24902496
///
2491-
/// This method is equivalent to calling the version that takes a generator,
2492-
/// passing in the system's default random generator.
2497+
/// This method is equivalent to calling `random(in:using:)`, passing in the
2498+
/// system's default random generator.
24932499
///
24942500
/// - Parameter range: The range in which to create a random value.
24952501
% if Range == 'Range':

stdlib/public/core/Integers.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2527,6 +2527,12 @@ extension FixedWidthInteger {
25272527
/// // Prints "44"
25282528
/// // Prints "21"
25292529
///
2530+
/// - Note: The algorithm used to create random values may change in a future
2531+
/// version of Swift. If you're passing a generator that results in the
2532+
/// same sequence of integer values each time you run your program, that
2533+
/// sequence may change when your program is compiled using a different
2534+
/// version of Swift.
2535+
///
25302536
/// - Parameters:
25312537
/// - range: The range in which to create a random value.
25322538
/// `range` must not be empty.
@@ -2649,8 +2655,8 @@ extension FixedWidthInteger {
26492655
/// // Prints "64"
26502656
/// // Prints "5"
26512657
///
2652-
/// This method is equivalent to calling the version that takes a generator,
2653-
/// passing in the system's default random generator.
2658+
/// This method is equivalent to calling `random(in:using:)`, passing in the
2659+
/// system's default random generator.
26542660
///
26552661
/// - Parameter range: The range in which to create a random value.
26562662
/// - Returns: A random value within the bounds of `range`.

0 commit comments

Comments
 (0)