--- id: LSN-0063 ticket: vm-owned-random-service title: Reproducible Random Is Stateful Transactional Work created: 2026-10-09 tags: [] --- ## Context Random generation is a useful example of deterministic work that cannot be modeled as a pure function of its visible arguments. A seed identifies the initial stream, but each call changes where the generator is in that stream. The runtime must preserve identity, call order, memory ownership and consumed work together if a cartridge is to replay the same logical behavior. This lesson derives from accepted decision DEC-0050, executed through PLN-0181. Its normative result is the [VM-owned random contract](../../../docs/specs/runtime/02e-vm-owned-random-core.md). Historical lineage is retained by the [discussion index](../../index.ndjson); completed control artifacts are disposable. The [qualification record](../../../docs/vm-arch/RANDOM_QUALIFICATION.md) contains executed environment, fixtures, checks and pending target comparisons. ## Key Decisions ### Identity belongs to the object, reproducibility to the ordered stream **What:** Random is an opaque reference builtin backed by a GC-managed object. Copies retain the same state; separate constructions with equal seed bits start at the same point and then evolve independently. Existing fields, arrays, Dynamic, captures and function boundaries store references, not generator copies. **Why:** This fits the published reference/ownership model and avoids accidental stream duplication through ordinary value copies. The frontend selects an intrinsic; it does not implement the generator or choose host entropy. **Trade-offs:** Consumers must distinguish aliasing from independent construction. Reproducible generators with the same seed are not evidence of statistical independence. Native snapshots do not become guest GC roots or import authority. ### Freeze the complete algorithm, including initialization and consumption **What:** The implementation uses PCG-XSL-RR 128/64 setseq with explicit wrapping, fixed increment and advance-before-output. Integer and Float calls consume the same sequence. Seed Int64 bits are zero-extended after u64 reinterpretation. **Why:** An algorithm family name alone is insufficient: variants, seed expansion, old-versus-new-state output and hidden buffers all change the stream. Float conversion also needs an exact endpoint rule; retaining the high 53 bits and scaling with software binary64 excludes 1 without a Float32 narrowing step. **Trade-offs:** Compatibility claims are limited to this exact contract. Independent canonical words plus a separate arithmetic oracle are stronger than comparing two instances of the same implementation. Debug/release equality is necessary; it does not qualify unexecuted operating systems or targets. ### Stage state changes but retain the cost of attempted work **What:** The interpreter validates all carriers before reference/bounds checks, reserves resources, draws on a private state candidate, and publishes the result and state only after success. Faults preserve operands and state while retaining cycles. Bounded range mapping charges a base plus every attempted word. **Why:** Popping operands first loses roots/owners and makes resource refusal or rejection-limit failure difficult to undo. Mutating public state while retrying creates an observable half-completed instruction. Refunding cycles, however, would erase work that the machine actually attempted. **Trade-offs:** Unbiased rejection may fail after its bounded work allowance. A cap fault leaves the generator at its original state; identical inputs/state will repeat the same failed candidate stream. Accepting the final permitted attempt and rejecting all permitted attempts are distinct cases. Whole-instruction budget overshoot remains sufficient; no new continuation/scheduler is needed. ## Patterns and Algorithms Treat a stateful intrinsic as a transaction over existing VM resources: retain source roots, validate in published order, reserve, prepare privately, then commit without fallible operations. A leaf payload does not require a new GC model: integrate its reference builtin with the existing ownership maps. Use the same shared catalog for signatures, admission, execution routing and exports. An explicit VM-owned implementation variant avoids giving host callbacks heap mutation authority. Publish guest-state effects separately from determinism, and publish variable cost structure rather than a misleading fixed base-only total. Rare paths require deliberate evidence. Test-only synthetic candidates execute the production bounded loop and transaction for multiple rejections, final acceptance and cap rollback. Real PCG fixtures separately prove the stream, including a natural rejection followed by acceptance. These two tests answer different questions and should not replace each other. ## Pitfalls - Treating deterministic=true as permission to fold or reorder stateful calls. - Copying a handle while expecting an independent stream. - Reducing unsigned words with simple modulus without the rejection threshold. - Computing an Int64 interval width in signed 64-bit arithmetic. - Rounding W/2^64 or narrowing to Float32 and accidentally including endpoint 1. - Committing generator state before result capacity or quota checks finish. - Advertising local reproducibility as cross-platform qualification or game readiness. ## Published Evidence and Handoffs The delivered core passed bytecode/VM suites in debug and release and the workspace regression suite. Twenty-four focused random tests cover guest fixtures and private engine/transaction behavior, including GC roots, rollback, resource ownership and exact costs. Metadata and published CSV agree; the constant catalog remains unchanged. Exact counts, fingerprints and commands belong to the qualification record, not to a normative lesson. The [producer handoff](../../../docs/vm-arch/random-producer-handoff.md) gives emitted PBX, expected result bits/cycles and consumer acceptance tests. Frontend projection/optimization, reference-game workloads and other-target comparison runs remain separate deliveries. No helper selection API was added.