Skip to content

Commit be88499

Browse files
authored
Merge pull request #19166 from natecook1000/nc42-random-notes
[4.2] [stdlib] Add notes about future changes to high-level random algorithms
2 parents c922f68 + 0efa5da commit be88499

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
@@ -102,6 +102,12 @@ public struct Bool {
102102
/// print("Maybe another try?")
103103
/// }
104104
///
105+
/// - Note: The algorithm used to create random values may change in a future
106+
/// version of Swift. If you're passing a generator that results in the
107+
/// same sequence of Boolean values each time you run your program, that
108+
/// sequence may change when your program is compiled using a different
109+
/// version of Swift.
110+
///
105111
/// - Parameter generator: The random number generator to use when creating
106112
/// the new random value.
107113
/// - Returns: Either `true` or `false`, randomly chosen with equal
@@ -124,9 +130,8 @@ public struct Bool {
124130
/// print("Maybe another try?")
125131
/// }
126132
///
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.
133+
/// This method is equivalent to calling `Bool.random(using:)`, passing in
134+
/// the system's default random generator.
130135
///
131136
/// - Returns: Either `true` or `false`, randomly chosen with equal
132137
/// probability.

stdlib/public/core/Collection.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,14 +1038,19 @@ extension Collection {
10381038
/// let randomName = names.randomElement(using: &myGenerator)!
10391039
/// // randomName == "Amani"
10401040
///
1041-
/// - Parameter generator: The random number generator to use when choosing
1042-
/// a random element.
1041+
/// - Parameter generator: The random number generator to use when choosing a
1042+
/// random element.
10431043
/// - Returns: A random element from the collection. If the collection is
10441044
/// empty, the method returns `nil`.
10451045
///
10461046
/// - Complexity: O(1) if the collection conforms to
1047-
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the length of
1048-
/// the collection.
1047+
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the length
1048+
/// of the collection.
1049+
/// - Note: The algorithm used to select a random element may change in a
1050+
/// future version of Swift. If you're passing a generator that results in
1051+
/// the same sequence of elements each time you run your program, that
1052+
/// sequence may change when your program is compiled using a different
1053+
/// version of Swift.
10491054
@inlinable
10501055
public func randomElement<T: RandomNumberGenerator>(
10511056
using generator: inout T
@@ -1068,15 +1073,15 @@ extension Collection {
10681073
/// let randomName = names.randomElement()!
10691074
/// // randomName == "Amani"
10701075
///
1071-
/// This method is equivalent to calling the version that takes a generator,
1072-
/// passing in the system's default random generator.
1076+
/// This method is equivalent to calling `randomElement(using:)`, passing in
1077+
/// the system's default random generator.
10731078
///
10741079
/// - Returns: A random element from the collection. If the collection is
10751080
/// empty, the method returns `nil`.
10761081
///
10771082
/// - Complexity: O(1) if the collection conforms to
1078-
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the length of
1079-
/// the collection.
1083+
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the length
1084+
/// of the collection.
10801085
@inlinable
10811086
public func randomElement() -> Element? {
10821087
var g = SystemRandomNumberGenerator()

stdlib/public/core/CollectionAlgorithms.swift

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,10 @@ extension Sequence {
387387
/// Returns the elements of the sequence, shuffled using the given generator
388388
/// as a source for randomness.
389389
///
390-
/// You use this method to randomize the elements of a sequence when you
391-
/// are using a custom random number generator. For example, you can shuffle
392-
/// the numbers between `0` and `9` by calling the `shuffled(using:)` method
393-
/// on that range:
390+
/// You use this method to randomize the elements of a sequence when you are
391+
/// using a custom random number generator. For example, you can shuffle the
392+
/// numbers between `0` and `9` by calling the `shuffled(using:)` method on
393+
/// that range:
394394
///
395395
/// let numbers = 0...9
396396
/// let shuffledNumbers = numbers.shuffled(using: &myGenerator)
@@ -401,6 +401,11 @@ extension Sequence {
401401
/// - Returns: An array of this sequence's elements in a shuffled order.
402402
///
403403
/// - Complexity: O(*n*), where *n* is the length of the sequence.
404+
/// - Note: The algorithm used to shuffle a sequence may change in a future
405+
/// version of Swift. If you're passing a generator that results in the
406+
/// same shuffled order each time you run your program, that sequence may
407+
/// change when your program is compiled using a different version of
408+
/// Swift.
404409
@inlinable
405410
public func shuffled<T: RandomNumberGenerator>(
406411
using generator: inout T
@@ -419,8 +424,8 @@ extension Sequence {
419424
/// let shuffledNumbers = numbers.shuffled()
420425
/// // shuffledNumbers == [1, 7, 6, 2, 8, 9, 4, 3, 5, 0]
421426
///
422-
/// This method is equivalent to calling the version that takes a generator,
423-
/// passing in the system's default random generator.
427+
/// This method is equivalent to calling `shuffled(using:)`, passing in the
428+
/// system's default random generator.
424429
///
425430
/// - Returns: A shuffled array of this sequence's elements.
426431
///
@@ -448,6 +453,11 @@ extension MutableCollection where Self : RandomAccessCollection {
448453
/// the collection.
449454
///
450455
/// - Complexity: O(*n*), where *n* is the length of the collection.
456+
/// - Note: The algorithm used to shuffle a collection may change in a future
457+
/// version of Swift. If you're passing a generator that results in the
458+
/// same shuffled order each time you run your program, that sequence may
459+
/// change when your program is compiled using a different version of
460+
/// Swift.
451461
@inlinable
452462
public mutating func shuffle<T: RandomNumberGenerator>(
453463
using generator: inout T
@@ -469,15 +479,14 @@ extension MutableCollection where Self : RandomAccessCollection {
469479

470480
/// Shuffles the collection in place.
471481
///
472-
/// Use the `shuffle()` method to randomly reorder the elements of an
473-
/// array.
482+
/// Use the `shuffle()` method to randomly reorder the elements of an array.
474483
///
475484
/// var names = ["Alejandro", "Camila", "Diego", "Luciana", "Luis", "Sofía"]
476485
/// names.shuffle(using: myGenerator)
477486
/// // names == ["Luis", "Camila", "Luciana", "Sofía", "Alejandro", "Diego"]
478487
///
479-
/// This method is equivalent to calling the version that takes a generator,
480-
/// passing in the system's default random generator.
488+
/// This method is equivalent to calling `shuffle(using:)`, passing in the
489+
/// system's default random generator.
481490
///
482491
/// - Complexity: O(*n*), where *n* is the length of the collection.
483492
@inlinable

stdlib/public/core/FloatingPoint.swift.gyb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2416,9 +2416,15 @@ where Self.RawSignificand : FixedWidthInteger {
24162416
///
24172417
/// The `random(in:using:)` static method chooses a random value from a
24182418
/// continuous uniform distribution in `range`, and then converts that value
2419-
/// to the nearest representable value in this type. Depending on the size and
2420-
/// span of `range`, some concrete values may be represented more frequently
2421-
/// than others.
2419+
/// to the nearest representable value in this type. Depending on the size
2420+
/// and span of `range`, some concrete values may be represented more
2421+
/// frequently than others.
2422+
///
2423+
/// - Note: The algorithm used to create random values may change in a future
2424+
/// version of Swift. If you're passing a generator that results in the
2425+
/// same sequence of floating-point values each time you run your program,
2426+
/// that sequence may change when your program is compiled using a
2427+
/// different version of Swift.
24222428
///
24232429
/// - Parameters:
24242430
/// - range: The range in which to create a random value.
@@ -2502,8 +2508,8 @@ where Self.RawSignificand : FixedWidthInteger {
25022508
/// of `range`, some concrete values may be represented more frequently than
25032509
/// others.
25042510
///
2505-
/// This method is equivalent to calling the version that takes a generator,
2506-
/// passing in the system's default random generator.
2511+
/// This method is equivalent to calling `random(in:using:)`, passing in the
2512+
/// system's default random generator.
25072513
///
25082514
/// - Parameter range: The range in which to create a random value.
25092515
% if Range == 'Range':

stdlib/public/core/Integers.swift.gyb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2628,6 +2628,12 @@ extension FixedWidthInteger {
26282628
/// // Prints "44"
26292629
/// // Prints "21"
26302630
///
2631+
/// - Note: The algorithm used to create random values may change in a future
2632+
/// version of Swift. If you're passing a generator that results in the
2633+
/// same sequence of integer values each time you run your program, that
2634+
/// sequence may change when your program is compiled using a different
2635+
/// version of Swift.
2636+
///
26312637
/// - Parameters:
26322638
/// - range: The range in which to create a random value.
26332639
% if Range == 'Range':
@@ -2689,8 +2695,8 @@ extension FixedWidthInteger {
26892695
/// // Prints "64"
26902696
/// // Prints "5"
26912697
///
2692-
/// This method is equivalent to calling the version that takes a generator,
2693-
/// passing in the system's default random generator.
2698+
/// This method is equivalent to calling `random(in:using:)`, passing in the
2699+
/// system's default random generator.
26942700
///
26952701
/// - Parameter range: The range in which to create a random value.
26962702
% if Range == 'Range':

0 commit comments

Comments
 (0)