Add the `random` module and an `os.urandom` host call

Merged
SA
3mo ago
+4193-142

Follow ups:

  • replace HostClock with AuthOsCalls or whatever that includes os.urandom
  • SessionState { globals, cwd, random } as described below

Summary by cubic

Adds a CPython-compatible random module and an os.urandom host call. Seeded generators match CPython 3.14; unseeded draws request host entropy rather than failing as unsupported.

Host entropy

  • The first draw from an unseeded generator requests one 2496-byte state vector; seeded generators never call the host.
  • os.urandom(size) rejects negative sizes, charges size before yielding, and requires an exact-size bytes response; the proto size is unsigned, so a negative count can't be expressed.
  • The default Python handler caps requests at 1 MiB with max_urandom_bytes; zero rejects nonempty requests without allocating.
  • Rejection loops poll deadlines, and sample, choices, and randbytes preflight allocations and saturate size calculations.

Random state

  • Implements MT19937, distributions, independent random.Random instances, and CPython-compatible seeding and state handling.
  • The module-level generator persists across REPL feeds and dumps (dump version 10).
  • random.Random instances and the class are returned through the public host API as repr strings rather than public Monty types.