mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Add _stdlib_random for more platforms (#1)
* Remove refs to Countable ranges * Add `_stdlib_random` for more platforms * Use `getrandom` (if available) for Android, Cygwin * Reorder the `_stdlib_random` functions * Also include <features.h> on Linux * Add `#error TODO` in `_stdlib_random` for Windows * Colon after Fatal Error Performance improvement for Random gybify ranges Fix typo in 'basic random numbers' Add _stdlib_random as a testable method Switch to generic constraints Hopefully link against bcrypt Fix some implementation details 1. Uniform distribution is now uniform 2. Apply Jens' method for uniform floats Fix a lineable attribute
This commit is contained in:
@@ -368,7 +368,7 @@ extension MutableCollection where Self : BidirectionalCollection {
|
||||
}
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// shuffled()
|
||||
// shuffled()/shuffle()
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension Sequence {
|
||||
@@ -377,14 +377,26 @@ extension Sequence {
|
||||
/// - Parameter generator: The random number generator to use when shuffling
|
||||
/// the sequence.
|
||||
/// - Returns: A shuffled array of this sequence's elements.
|
||||
@_inlineable
|
||||
public func shuffled(
|
||||
using generator: RandomNumberGenerator = Random.default
|
||||
@inlinable
|
||||
public func shuffled<T: RandomNumberGenerator>(
|
||||
using generator: T
|
||||
) -> [Element] {
|
||||
var result = ContiguousArray(self)
|
||||
result.shuffle(using: generator)
|
||||
return Array(result)
|
||||
}
|
||||
|
||||
/// Returns the elements of the sequence, shuffled.
|
||||
///
|
||||
/// - Parameter generator: The random number generator to use when shuffling
|
||||
/// the sequence.
|
||||
/// - Returns: A shuffled array of this sequence's elements.
|
||||
///
|
||||
/// This uses the standard library's default random number generator.
|
||||
@inlinable
|
||||
public func shuffled() -> [Element] {
|
||||
return shuffled(using: Random.default)
|
||||
}
|
||||
}
|
||||
|
||||
extension MutableCollection {
|
||||
@@ -392,9 +404,9 @@ extension MutableCollection {
|
||||
///
|
||||
/// - Parameter generator: The random number generator to use when shuffling
|
||||
/// the collection.
|
||||
@_inlineable
|
||||
public mutating func shuffle(
|
||||
using generator: RandomNumberGenerator = Random.default
|
||||
@inlinable
|
||||
public mutating func shuffle<T: RandomNumberGenerator>(
|
||||
using generator: T
|
||||
) {
|
||||
guard count > 1 else { return }
|
||||
var amount = count
|
||||
@@ -409,6 +421,17 @@ extension MutableCollection {
|
||||
formIndex(after: ¤tIndex)
|
||||
}
|
||||
}
|
||||
|
||||
/// Shuffles the collection in place.
|
||||
///
|
||||
/// - Parameter generator: The random number generator to use when shuffling
|
||||
/// the collection.
|
||||
///
|
||||
/// This uses the standard library's default random number generator.
|
||||
@inlinable
|
||||
public mutating func shuffle() {
|
||||
shuffle(using: Random.default)
|
||||
}
|
||||
}
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
Reference in New Issue
Block a user