Improve random() to use name-based salting for order-independent reproducibility#434
Closed
Improve random() to use name-based salting for order-independent reproducibility#434
Conversation
…oducibility Replace the global execution counter with variable-name-based salting to prevent the "ripple effect" where adding, removing, or reordering variables changes random values for ALL subsequent variables. Changes: - Add _stable_string_hash() for deterministic hashing across Python processes - Modify random() to use tracer stack variable name instead of global counter - Add optional salt parameter for use outside formula context - Update tests to verify order independence and reproducibility Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
The local black version (25.12.0) formats differently than CI's black. Reverting this unrelated file to avoid CI lint failure. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Collaborator
Author
|
If |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I've done a complete 180 on the random function. I now believe it should be the bedrock of randomness. The current version is pretty close but fragile in some ways that are easily fixed; hence this PR.
A separate PR in -us is coming that shows the new pattern using randomness and "seed" variables, renamed to "draw".
With the current random(), if you add a new variable that uses random() → every subsequent variable's random values shift. This PR uses an entity id plus variable "salt" pattern that makes random values stable per id and variable - adding/removing/reordering variables doesn't affect other variables' random draws.
Summary
_stable_string_hash()for deterministic hashing across Python processessaltparameter for use outside formula contextFixes #433, #363, #412
Breaking Change
This will change random values for all existing simulations using
random(). The old seed formula was:The new formula is:
Test plan
🤖 Generated with Claude Code