Skip to content

feat(stdlib): STEP 4-B randomness + perf.now bindings (Refs #239, closes standards#327)#509

Open
hyperpolymath wants to merge 1 commit into
mainfrom
claude/step4-b-random-perf
Open

feat(stdlib): STEP 4-B randomness + perf.now bindings (Refs #239, closes standards#327)#509
hyperpolymath wants to merge 1 commit into
mainfrom
claude/step4-b-random-perf

Conversation

@hyperpolymath
Copy link
Copy Markdown
Owner

Adds the randomness + high-res-timer surface that estate property tests and benchmark fixtures need:

  • bofig/tests/property/graph_properties_test.ts (377L) substitutes \${prefix}_${Math.floor(Math.random() * 1000000)}`` for ID generation.
  • Bench tests want sub-millisecond timings (performance.now()).

What lands

stdlib/Deno.affine (+4 externs)

extern lowers to notes
math_random() -> Float Math.random() [0, 1). JS PRNG, non-crypto.
random_u32() -> Int ((Math.random() * 4294967296) >>> 0) uniform u32
random_in_range(lo, hi) -> Int Math.floor(Math.random() * (hi - lo)) + lo uniform [lo, hi)
performance_now() -> Float performance.now() high-res sub-ms timer

math_random is the JS PRNG — not cryptographically secure. Sufficient for property-test input generation, sampling, and simulations. For cryptographic random bytes a separate crypto_random_bytes binding routing to crypto.getRandomValues() belongs in a different sub-issue (different host call, different threat model).

Tests

tests/codegen-deno/random_smoke.{affine,deno.js,harness.mjs} covers:

  • 1000 math_random draws all in [0, 1)
  • 10000 random_u32 draws all in [0, 2^32) with ≥ 1000 distinct values (catches degenerate-PRNG regression)
  • 1000 random_in_range(0, 100) draws all in [0, 100)
  • 500 random_in_range(50, 60) draws cover most of the window
  • performance_now monotone non-decreasing across three consecutive calls (catches clock-resolution regression)

Verification

step result
dune build bin/main.exe
dune runtest ✅ 353/353
./tools/run_codegen_deno_tests.sh ✅ all harnesses (incl. the new one)

Out of scope

  • Seeded RNG (@std/random) — defer until a determinism-needing property test surfaces.
  • sleep / setTimeout — separate effect surface; not blocking STEP 4.

Relation to #504 + #507

The three STEP 3 / STEP 4-A / STEP 4-B PRs all add disjoint externs to stdlib/Deno.affine and lib/codegen_deno.ml. Each is independently mergeable from origin/main; expected merge order is #504#507 → this PR but the file sections are separate enough that the rebases are mechanical.

Refs

  • Closes standards#327
  • Refs: standards#239 (umbrella), standards#243 (STEP 4 per-repo unblock target), affinescript#504 (STEP 3 sibling), affinescript#507 (STEP 4-A sibling)

🤖 Generated with Claude Code

…ses #327)

Adds the randomness + high-res-timer surface that estate property tests
and benchmark fixtures need (bofig property tests substitute
`Math.floor(Math.random() * 1000000)` for ID generation; benchmark
tests want sub-millisecond timings).

Stdlib (`stdlib/Deno.affine`):
  + math_random()                 -> Math.random()
  + random_u32()                  -> ((Math.random() * 2^32) >>> 0)
  + random_in_range(lo, hi)       -> Math.floor(Math.random() * (hi-lo)) + lo
  + performance_now()             -> performance.now()

`math_random` is the JS PRNG — non-crypto. Sufficient for property-test
input generation, sampling, and simulations. Cryptographic random bytes
need a separate `crypto_random_bytes` binding routing to
`crypto.getRandomValues()` (different host call, different threat
model — file as separate sub-issue when needed).

Tests:
  + tests/codegen-deno/random_smoke.{affine,deno.js,harness.mjs}
    - 1000 math_random draws all in [0, 1)
    - 10000 random_u32 draws all in [0, 2^32) with >= 1000 distinct values
    - 1000 random_in_range(0, 100) draws all in [0, 100)
    - 500 random_in_range(50, 60) draws cover most of the window
    - performance_now monotone-non-decreasing across consecutive calls
  ✓ All 353 dune-runtest tests pass.
  ✓ All codegen-Deno-ESM harnesses pass.

Out of scope:
  - Seeded RNG (`@std/random`) — defer until determinism-needing
    property test exists.
  - sleep / setTimeout — separate effect surface, not blocking STEP 4.

Refs: hyperpolymath/standards#239 (umbrella), hyperpolymath/standards#327

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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