Skip to content

Seed the random generator only when rand() needs it - #682

Merged
bertysentry merged 1 commit into
mainfrom
claude/issue-677-lazy-random-seed
Nov 29, 2026
Merged

bertysentry merged 1 commit into
mainfrom
claude/issue-677-lazy-random-seed

Conversation

@bertysentry

Copy link
Copy Markdown
Contributor

Fixes #677

Problem

Every eval() and execute() resets the runtime with randomNumberGenerator.setSeed(1). BSDRandom.setSeed fills the state table and discards 310 values, which the issue measures at about half the cost of a small eval(), although very few programs call rand().

Change

The laziness lives in BSDRandom itself, so every caller benefits and AVM.java is not touched:

  • setSeed() only records the seed and marks it as pending.
  • nextDouble() builds the state table from the recorded seed first when one is pending.
  • getSeed() is unchanged: it returns the last seed supplied, so srand() still returns the previous seed (1 after a reset).

The sequence of rand() values is unchanged: the same seeding code runs, only later. There is no user-visible behavior change, so behavior-changes.md has no new entry.

Tests

  • BSDRandomTest (plain JUnit): seed reported before any value is drawn, reseeding restarts the sequence, reseeding after use matches a fresh generator, seed 0 behaves like seed 1, different seeds give different sequences.
  • RandomSeedResetTest: one runtime reused for several executions (through AwkTestSupport), with and without rand(), direct and indirect (@f()) calls, and the values returned by srand(); plus the same check across several Awk.eval() calls.

mvn verify passes locally: 1028 unit tests, and checkstyle, pmd and spotbugs report nothing.


Generated by Claude Code

BSDRandom.setSeed() now only records the seed. The state table is built,
and the first 310 values discarded, when the next value is requested.
A runtime reset by eval() or execute() no longer pays for a seeding that
most programs never use. The sequence of rand() values and the values
returned by srand() are unchanged.

Fixes #677

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012oN69BYSPd66efoyXEYoRD
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-29T08:21:30.319985Z 948eac4 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@bertysentry
bertysentry merged commit eead5a7 into main Nov 29, 2026
5 checks passed
@bertysentry
bertysentry deleted the claude/issue-677-lazy-random-seed branch November 29, 2026 12:07
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.

Seed the random generator only when rand() needs it

2 participants