Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use rng initialized with seedrandom (alea) #2992

Merged
merged 4 commits into from Jan 23, 2024
Merged

Use rng initialized with seedrandom (alea) #2992

merged 4 commits into from Jan 23, 2024

Conversation

berekuk
Copy link
Collaborator

@berekuk berekuk commented Jan 21, 2024

Fixes #248.

Seed is optional in environment, and not exposed to the UI yet. Tests are still random too.

seedrandom.alea is faster than Math.random() in Node v20 (I'm not sure about Firefox):

  • Old code: 0.66s to create 2 to 3 dist with 10M samples.
  • New code: 0.58s.

@berekuk berekuk requested a review from OAGr as a code owner January 21, 2024 01:29
Copy link

changeset-bot bot commented Jan 21, 2024

🦋 Changeset detected

Latest commit: 75c1db9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 6 packages
Name Type
@quri/squiggle-components Patch
@quri/squiggle-lang Patch
@quri/versioned-squiggle-components Patch
vscode-squiggle Patch
@quri/prettier-plugin-squiggle Patch
@quri/squiggle-textmate-grammar Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Jan 21, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
quri-hub ✅ Ready (Inspect) Visit Preview Jan 23, 2024 8:26pm
squiggle-components ✅ Ready (Inspect) Visit Preview Jan 23, 2024 8:26pm
squiggle-website ✅ Ready (Inspect) Visit Preview Jan 23, 2024 8:26pm
1 Ignored Deployment
Name Status Preview Updated (UTC)
quri-ui ⬜️ Ignored (Inspect) Visit Preview Jan 23, 2024 8:26pm

@@ -81,6 +81,9 @@ const interpolate = (
yMax: number,
xIntended: number
): number => {
if (yMin === yMax) {
return yMin;
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is important; yMin * minProportion + yMax * maxProportion was precise with Math.random() values which ended with 0 bit, but not with other RNGs.

@@ -59,6 +61,7 @@ function throwFrom(
* `context.evaluate` can inject additional behaviors, e.g. delay for pseudo-async evaluation.
*/
export const evaluate: ReducerFn = (expression, context) => {
jstat.setRandom(context.rng);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jstat is global, so this is stateful, but as long as evaluate is not async, it's good enough.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. A bit scary, we'll just have to remember if we go async or parallelize

@@ -40,15 +40,6 @@ export abstract class SqAbstractDistribution<T extends BaseDist> {
);
}

asSampleSetDist(env: Env) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of requiring rng here, I removed this method, it wasn't used anywhere.


xDist(env: Env): Result.result<SqSampleSetDistribution, SqError> {
return this.buildSampleSetDist(this._value.xDist, env);
xDist(): SqSampleSetDistribution {
Copy link
Collaborator Author

@berekuk berekuk Jan 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a nice coincidence that scatter plots require sample set dists now, otherwise I'd have to require rng prop, which is hard to obtain in components.

@OAGr
Copy link
Contributor

OAGr commented Jan 22, 2024

Seems pretty great, happy that this was so straightforward.

I don't like that this seems to make a regression to Squiggle hub -> Now, there doesn't seem to be any randomness between runs. I could see why we'd want this, but it's not apparent to the user, which is kind of scary to me. I'd expect that every time I click "run", the seed would change, unless I made it fixed.

How about now we randomly set the seed each "run", then later we make the seed configurable?

Happy for it to be merged otherwise.

@berekuk
Copy link
Collaborator Author

berekuk commented Jan 23, 2024

I'd expect that every time I click "run", the seed would change, unless I made it fixed.

This was actually a bug! Autoseeding didn't work, I fixed it in 75c1db9.

@github-actions github-actions bot mentioned this pull request Jan 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Expose random seed fixing to developer (mostly for testing)
2 participants