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:
Ben Rimmington
2018-02-10 00:09:14 +00:00
committed by Azoy
parent d23d219e95
commit a5df0ef83d
11 changed files with 342 additions and 183 deletions

View File

@@ -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: &currentIndex)
}
}
/// 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)
}
}
//===----------------------------------------------------------------------===//