|
2 | 2 | // |
3 | 3 | // This source file is part of the Swift.org open source project |
4 | 4 | // |
5 | | -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 5 | +// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors |
6 | 6 | // Licensed under Apache License v2.0 with Runtime Library Exception |
7 | 7 | // |
8 | 8 | // See https://swift.org/LICENSE.txt for license information |
@@ -803,6 +803,25 @@ public protocol Collection: Sequence where SubSequence: Collection { |
803 | 803 | /// `endIndex`. |
804 | 804 | func formIndex(after i: inout Index) |
805 | 805 |
|
| 806 | + /// Returns a random element of the collection, using the given generator as |
| 807 | + /// a source for randomness. |
| 808 | + /// |
| 809 | + /// You use this method to select a random element from a collection when you |
| 810 | + /// are using a custom random number generator. For example, call |
| 811 | + /// `randomElement(using:)` to select a random element from an array of names. |
| 812 | + /// |
| 813 | + /// let names = ["Zoey", "Chloe", "Amani", "Amaia"] |
| 814 | + /// let randomName = names.randomElement(using: &myGenerator)! |
| 815 | + /// // randomName == "Amani" (maybe) |
| 816 | + /// |
| 817 | + /// - Parameter generator: The random number generator to use when choosing |
| 818 | + /// a random element. |
| 819 | + /// - Returns: A random element from the collection. If the collection is |
| 820 | + /// empty, the method returns `nil`. |
| 821 | + func randomElement<T: RandomNumberGenerator>( |
| 822 | + using generator: inout T |
| 823 | + ) -> Element? |
| 824 | + |
806 | 825 | @available(*, deprecated, message: "all index distances are now of type Int") |
807 | 826 | typealias IndexDistance = Int |
808 | 827 | } |
@@ -1016,6 +1035,54 @@ extension Collection { |
1016 | 1035 | return count |
1017 | 1036 | } |
1018 | 1037 |
|
| 1038 | + /// Returns a random element of the collection, using the given generator as |
| 1039 | + /// a source for randomness. |
| 1040 | + /// |
| 1041 | + /// You use this method to select a random element from a collection when you |
| 1042 | + /// are using a custom random number generator. For example, call |
| 1043 | + /// `randomElement(using:)` to select a random element from an array of names. |
| 1044 | + /// |
| 1045 | + /// let names = ["Zoey", "Chloe", "Amani", "Amaia"] |
| 1046 | + /// let randomName = names.randomElement(using: &myGenerator)! |
| 1047 | + /// // randomName == "Amani" (maybe) |
| 1048 | + /// |
| 1049 | + /// - Parameter generator: The random number generator to use when choosing |
| 1050 | + /// a random element. |
| 1051 | + /// - Returns: A random element from the collection. If the collection is |
| 1052 | + /// empty, the method returns `nil`. |
| 1053 | + @inlinable |
| 1054 | + public func randomElement<T: RandomNumberGenerator>( |
| 1055 | + using generator: inout T |
| 1056 | + ) -> Element? { |
| 1057 | + guard !isEmpty else { return nil } |
| 1058 | + let random = generator.next(upperBound: UInt(count)) |
| 1059 | + let index = self.index( |
| 1060 | + startIndex, |
| 1061 | + offsetBy: numericCast(random) |
| 1062 | + ) |
| 1063 | + return self[index] |
| 1064 | + } |
| 1065 | + |
| 1066 | + /// Returns a random element of the collection. |
| 1067 | + /// |
| 1068 | + /// For example, call `randomElement()` to select a random element from an |
| 1069 | + /// array of names. |
| 1070 | + /// |
| 1071 | + /// let names = ["Zoey", "Chloe", "Amani", "Amaia"] |
| 1072 | + /// let randomName = names.randomElement()! |
| 1073 | + /// // randomName == "Amani" (perhaps) |
| 1074 | + /// |
| 1075 | + /// This method uses the default random generator, `Random.default`. The call |
| 1076 | + /// to `names.randomElement()` above is equivalent to calling |
| 1077 | + /// `names.randomElement(using: &Random.default)`. |
| 1078 | + /// |
| 1079 | + /// - Returns: A random element from the collection. If the collection is |
| 1080 | + /// empty, the method returns `nil`. |
| 1081 | + @inlinable |
| 1082 | + public func randomElement() -> Element? { |
| 1083 | + return randomElement(using: &Random.default) |
| 1084 | + } |
| 1085 | + |
1019 | 1086 | /// Do not use this method directly; call advanced(by: n) instead. |
1020 | 1087 | @inlinable |
1021 | 1088 | @inline(__always) |
|
0 commit comments