-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[stdlib] Random unification #16413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[stdlib] Random unification #16413
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d23d219
Add shims for stdlib random
Azoy a5df0ef
Add `_stdlib_random` for more platforms (#1)
benrimmington f146d17
Revise documentation, add benchmarks (#3)
natecook1000 b65d0c1
Consolidate `_stdlib_random` functions (#2)
benrimmington 12a2b32
[stdlib] Add & implement Random._fill(bytes:) requirement
lorentey 54b3b8b
[stdlib] Random: don't randomize FixedWidthIntegers by overwriting th…
lorentey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| //===--- RandomShuffle.swift ----------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2018 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
| // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import TestsUtils | ||
|
|
||
| // | ||
| // Benchmark that shuffles arrays of integers. Measures the performance of | ||
| // shuffling large arrays. | ||
| // | ||
|
|
||
| public let RandomShuffle = [ | ||
| BenchmarkInfo(name: "RandomShuffleDef", runFunction: run_RandomShuffleDef, | ||
| tags: [.api], setUpFunction: setup_RandomShuffle), | ||
| BenchmarkInfo(name: "RandomShuffleLCG", runFunction: run_RandomShuffleLCG, | ||
| tags: [.api], setUpFunction: setup_RandomShuffle), | ||
| ] | ||
|
|
||
| /// A linear congruential PRNG. | ||
| struct LCRNG: RandomNumberGenerator { | ||
| private var state: UInt64 | ||
|
|
||
| init(seed: Int) { | ||
| state = UInt64(truncatingIfNeeded: seed) | ||
| for _ in 0..<10 { _ = next() } | ||
| } | ||
|
|
||
| mutating func next() -> UInt64 { | ||
| state = 2862933555777941757 &* state &+ 3037000493 | ||
| return state | ||
| } | ||
| } | ||
|
|
||
| var numbers = Array(0...3_000_000) | ||
|
|
||
| @inline(never) | ||
| func setup_RandomShuffle() { | ||
| _ = numbers.count | ||
| } | ||
|
|
||
| @inline(never) | ||
| public func run_RandomShuffleDef(_ N: Int) { | ||
| for _ in 0 ..< N { | ||
| numbers.shuffle() | ||
| blackHole(numbers.first!) | ||
| } | ||
| } | ||
|
|
||
| @inline(never) | ||
| public func run_RandomShuffleLCG(_ N: Int) { | ||
| var generator = LCRNG(seed: 0) | ||
| for _ in 0 ..< N { | ||
| numbers.shuffle(using: &generator) | ||
| blackHole(numbers.first!) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| //===--- RandomValues.swift -----------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2018 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
| // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import TestsUtils | ||
|
|
||
| // | ||
| // Benchmark generating lots of random values. Measures the performance of | ||
| // the default random generator and the algorithms for generating integers | ||
| // and floating-point values. | ||
| // | ||
|
|
||
| public let RandomValues = [ | ||
| BenchmarkInfo(name: "RandomIntegersDef", runFunction: run_RandomIntegersDef, tags: [.api]), | ||
| BenchmarkInfo(name: "RandomIntegersLCG", runFunction: run_RandomIntegersLCG, tags: [.api]), | ||
| BenchmarkInfo(name: "RandomDoubleDef", runFunction: run_RandomDoubleDef, tags: [.api]), | ||
| BenchmarkInfo(name: "RandomDoubleLCG", runFunction: run_RandomDoubleLCG, tags: [.api]), | ||
| ] | ||
|
|
||
| /// A linear congruential PRNG. | ||
| struct LCRNG: RandomNumberGenerator { | ||
| private var state: UInt64 | ||
|
|
||
| init(seed: Int) { | ||
| state = UInt64(truncatingIfNeeded: seed) | ||
| for _ in 0..<10 { _ = next() } | ||
| } | ||
|
|
||
| mutating func next() -> UInt64 { | ||
| state = 2862933555777941757 &* state &+ 3037000493 | ||
| return state | ||
| } | ||
| } | ||
|
|
||
| @inline(never) | ||
| public func run_RandomIntegersDef(_ N: Int) { | ||
| for _ in 0 ..< N { | ||
| var x = 0 | ||
| for _ in 0 ..< 100_000 { | ||
| x &+= Int.random(in: 0...10_000) | ||
| } | ||
| blackHole(x) | ||
| } | ||
| } | ||
|
|
||
| @inline(never) | ||
| public func run_RandomIntegersLCG(_ N: Int) { | ||
| for _ in 0 ..< N { | ||
| var x = 0 | ||
| var generator = LCRNG(seed: 0) | ||
| for _ in 0 ..< 100_000 { | ||
| x &+= Int.random(in: 0...10_000, using: &generator) | ||
| } | ||
| CheckResults(x == 498214315) | ||
| } | ||
| } | ||
|
|
||
| @inline(never) | ||
| public func run_RandomDoubleDef(_ N: Int) { | ||
| for _ in 0 ..< N { | ||
| var x = 0.0 | ||
| for _ in 0 ..< 100_000 { | ||
| x += Double.random(in: -1000...1000) | ||
| } | ||
| blackHole(x) | ||
| } | ||
| } | ||
|
|
||
| @inline(never) | ||
| public func run_RandomDoubleLCG(_ N: Int) { | ||
| for _ in 0 ..< N { | ||
| var x = 0.0 | ||
| var generator = LCRNG(seed: 0) | ||
| for _ in 0 ..< 100_000 { | ||
| x += Double.random(in: -1000...1000, using: &generator) | ||
| } | ||
| blackHole(x) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: why
>> 17?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't totally insane, because for (some) very weak RNGs middle bits have better randomness properties than low bits, but really no one should be using those ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nate sent this in the initial pr:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it; seemed strange because
17is pretty arbitrary. If we want to use high bits,Int(bitPattern: generator.next()) < 0would be less mystifying, IMO.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xwu you don't want the high bits either, those can also be weak. You really want some "middle" bits. This is worth a comment, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Go ahead, try to name a number more random than
17.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, according to some cursory testing, the
GKRandomSourcetypes use31for the offset innextBool()(i.e., the high bit, like @xwu suggested).