-
Notifications
You must be signed in to change notification settings - Fork 40
Add bytestream-based 'random' module with PRNG and /dev/urandom handlers & fix float ops on Chez #966
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
Add bytestream-based 'random' module with PRNG and /dev/urandom handlers & fix float ops on Chez #966
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
093a3ec
Add a PRNG-based 'random' module
jiribenes e9ae19a
Finalise the module
jiribenes 39163f5
Add a simple unit test
jiribenes ba3efe1
Try to fix float division on Chez
jiribenes 386903c
Modify tests
jiribenes 9d9b8b9
Sync JS and LLVM
jiribenes 5745f59
Remove unused function, add randomDoubles
jiribenes dccf51f
Fix 'round' on Chez
jiribenes e97cd3f
Regenerate tests (oops)
jiribenes bcdcf28
Improve doc comments
jiribenes 9058eca
Address review comments
jiribenes d58e39f
Don't run /dev/urandom in tests
jiribenes 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,97 @@ | ||
| module random | ||
|
|
||
| import stream | ||
|
|
||
| /// A streaming source of byte-level randomness. | ||
| def minstd(seed: Int): Unit / emit[Byte] = { | ||
| // Initialize state with seed, ensuring it's not zero | ||
| var state = if (seed == 0) 1 else seed | ||
|
|
||
| def nextInt(): Int = { | ||
| // Park-Miller minimal standard PRNG | ||
| // Uses only at most 32-bit integers internally | ||
| val a = 48271 | ||
| val m = 2147483647 | ||
|
|
||
| val q = m / a // 44488 | ||
| val r = m.mod(a) // 3399 | ||
|
|
||
| val div = state / q // max: M / Q = A = 48,271 | ||
| val rem = state.mod(q) // max: Q - 1 = 44,487 | ||
|
|
||
| val s = rem * a; // max: 44,487 * 48,271 = 2,147,431,977 = 0x7fff3629 | ||
| val t = div * r; // max: 48,271 * 3,399 = 164,073,129 | ||
|
|
||
| val result = s - t | ||
| // keep the state positive | ||
| if (result < 0) result + m else result | ||
| } | ||
|
|
||
| while (true) { | ||
| state = nextInt() | ||
| val b = state.mod(256).toByte | ||
| do emit(b) | ||
| } | ||
| } | ||
|
|
||
| /// A think wrapper over `minstd` and `stream::source` | ||
| def minstd(seed: Int) { randomnessReader: () => Unit / read[Byte] }: Unit = | ||
| source[Byte] { minstd(seed) } {randomnessReader} | ||
|
|
||
| // randomness functions | ||
|
|
||
| def randomBool(): Bool / {read[Byte], stop} = { | ||
| val b = do read[Byte]() | ||
| b.toInt.mod(2) == 1 | ||
| } | ||
|
|
||
| def randomInt32(): Int / {read[Byte], stop} = { | ||
| var result = 0 | ||
| repeat(4) { | ||
| val b = do read[Byte]() | ||
| result = result.bitwiseShl(8).bitwiseOr(b.toInt) | ||
| } | ||
| result | ||
| } | ||
jiribenes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def randomInt64(): Int / {read[Byte], stop} = { | ||
| var result = 0 | ||
| repeat(8) { | ||
| val b = do read[Byte]() | ||
| result = result.bitwiseShl(8).bitwiseOr(b.toInt) | ||
| } | ||
| result | ||
| } | ||
|
|
||
| /// `max` is _inclusive_! | ||
| def randomInt(min: Int, max: Int): Int / {read[Byte], stop} = { | ||
| if (min > max) { | ||
| randomInt(max, min) | ||
| } else { | ||
| val range = max - min + 1 | ||
| val bytesNeeded = (log(range.toDouble) / log(256.0)).ceil | ||
|
|
||
| var result = 0 | ||
| repeat(bytesNeeded) { | ||
| val b = do read[Byte]() | ||
| result = result.bitwiseShl(8).bitwiseOr(b.toInt) | ||
| } | ||
|
|
||
| min + (abs(result).mod(range)) | ||
| } | ||
| } | ||
|
|
||
| namespace examples { | ||
| def main() = { | ||
| with boundary; | ||
| with minstd(1337); | ||
|
|
||
| repeat(10) { | ||
| val a = randomInt(1, 10) | ||
| val b = randomInt(1, 100) | ||
| val c = randomInt(1, 1000) | ||
| println(a.show ++ " " ++ b.show ++ " " ++ c.show) | ||
| println(a + b + c) | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.