Add crypto.random-int for random integers in a range #275

Closed
opened 2026-01-18 16:30:13 +00:00 by navicore · 1 comment
navicore commented 2026-01-18 16:30:13 +00:00 (Migrated from github.com)

Summary

Add a builtin for generating cryptographically secure random integers within a range.

Proposed API

crypto.random-int  ( min max -- Int )   # uniform random in [min, max)

Example:

0 100 crypto.random-int   # random int from 0 to 99
1 7 crypto.random-int     # random int from 1 to 6 (dice roll)

Current Workaround

Users must currently:

  1. Use crypto.random-bytes to get hex string
  2. Convert hex to int (no easy builtin for this)
  3. Use modulo (introduces bias for non-power-of-2 ranges)

Implementation Notes

  • Use rejection sampling to avoid modulo bias
  • Should use the same CSPRNG as crypto.random-bytes
  • Consider edge cases: min >= max should error or return min

Signature

builtin!(sigs, "crypto.random-int", (a Int Int -- a Int));
## Summary Add a builtin for generating cryptographically secure random integers within a range. ## Proposed API ``` crypto.random-int ( min max -- Int ) # uniform random in [min, max) ``` Example: ```seq 0 100 crypto.random-int # random int from 0 to 99 1 7 crypto.random-int # random int from 1 to 6 (dice roll) ``` ## Current Workaround Users must currently: 1. Use `crypto.random-bytes` to get hex string 2. Convert hex to int (no easy builtin for this) 3. Use modulo (introduces bias for non-power-of-2 ranges) ## Implementation Notes - Use rejection sampling to avoid modulo bias - Should use the same CSPRNG as `crypto.random-bytes` - Consider edge cases: min >= max should error or return min ## Signature ```rust builtin!(sigs, "crypto.random-int", (a Int Int -- a Int)); ```
navicore commented 2026-01-18 18:40:32 +00:00 (Migrated from github.com)
https://github.com/navicore/patch-seq/pull/278
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
navicore/patch-seq#275
No description provided.