Skip to content

Conversation

jiribenes
Copy link
Contributor

@jiribenes jiribenes commented Apr 28, 2025

Straightforward streaming randomness based on bytes, see the examples namespace.
The goal is to unblock #893.

The generator is specialised for 32-bit numbers, always returning only the lowest byte (which might be bad / inefficient, but oh well).
Here's more about the algorithm https://en.wikipedia.org/wiki/Lehmer_random_number_generator

Goals:

  • working, idiomatic PRNG
  • works across all backends (doesn't rely on a bit-width of Int through weird shifting behaviour...)
  • easy(-ish) to use

Non-goals:

  • good performance
  • cryptographic safety

Comment on lines 163 to 171
def unixRandom(): Unit = {
with boundary;
with on[IOError].report;
with devurandom;

repeat(2) {
println(randomInt32())
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not actually called for testing purposes (since how would we figure out if it works?), but serves as a quick demo.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we'd want, we could add a test that just asserts that we get some values (not an error).

Note that on Mac/M1, this leads to segfaults for me on LLVM 🤔 (but probably that's the known problem)

@jiribenes jiribenes marked this pull request as ready for review April 29, 2025 11:36
@jiribenes jiribenes removed the request for review from marzipankaiser April 29, 2025 11:48
@jiribenes jiribenes marked this pull request as draft April 29, 2025 11:48
@jiribenes jiribenes changed the title Add a PRNG-based 'random' module Add a PRNG-based 'random' module & fix float ops on Chez Apr 29, 2025
@jiribenes jiribenes marked this pull request as ready for review April 29, 2025 12:38
@jiribenes jiribenes changed the title Add a PRNG-based 'random' module & fix float ops on Chez Add bytestream-based 'random' module with PRNG and /dev/urandom handlers & fix float ops on Chez Apr 29, 2025
Copy link
Contributor

@marzipankaiser marzipankaiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Generated randomness also seems to be OK (at least for my superfluous testing).

Comment on lines 163 to 171
def unixRandom(): Unit = {
with boundary;
with on[IOError].report;
with devurandom;

repeat(2) {
println(randomInt32())
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we'd want, we could add a test that just asserts that we get some values (not an error).

Note that on Mac/M1, this leads to segfaults for me on LLVM 🤔 (but probably that's the known problem)

@jiribenes jiribenes merged commit 8152c6b into master Apr 29, 2025
3 checks passed
@jiribenes jiribenes deleted the stdlib/random branch April 29, 2025 13:57
lenakaeufel pushed a commit to lenakaeufel/effekt that referenced this pull request May 1, 2025
…ers & fix float ops on Chez (effekt-lang#966)

Straightforward streaming randomness with an interface
supporting both bytes-based PRNG and /dev/urandom.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants