Skip to content

Conversation

@cktgh
Copy link

@cktgh cktgh commented Jan 7, 2026

Currently there is NO easy/accurate way to generate random values of a specific range.
The existing "Pseudo-Random Number Generator" allows generation of bytes only, and processing that result into values of a specific range is difficult (see attempt at #1954).

This pull request adds "Pseudo-Random Integer Generator", an operation for generating random integers of a user defined range.

2026_01_07__123723
  • Accurate outputs with no "modulo bias", by using the "rejection sample" method.
  • Support values ranging from Number.MIN_SAFE_INTEGER to Number.MAX_SAFE_INTEGER.
  • Support generating multiple values at a time with a delimiter option.
  • Uses Crypto.getRandomValues() under the hood for cryptographic security. This is also consistent with the existing "Pseudo-Random Number Generator".

 


Design Notes

  • Avoided the use of BigInt during random bytes generation for performance. This implementation stitches 32 bit integer values together under the hood.
  • Did not change the existing "Pseudo-Random Number Generator" at all.
    • Separate from "Pseudo-Random Number Generator" to avoid dealing with compatibility issues for the existing recipes.
    • I think its best to add a "mode" option under "Pseudo-Random Number Generator", and in theory we could just add the new arguments at the back. I actually did that at first and it looked really ugly so I separated it into this new operation instead.
  • Tests are not added because its not really necessary, and besides "Pseudo-Random Number Generator" doesn't have tests neither. 🤷‍♂️

@cktgh cktgh force-pushed the feat/random-int branch 2 times, most recently from d6a6355 to 8fa2f4d Compare January 7, 2026 02:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant