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

Avoid seek leaking by crating local prng #4

Closed
vorg opened this issue Jan 5, 2018 · 1 comment · Fixed by #5
Closed

Avoid seek leaking by crating local prng #4

vorg opened this issue Jan 5, 2018 · 1 comment · Fixed by #5

Comments

@vorg
Copy link
Member

vorg commented Jan 5, 2018

var myrng = new Math.seedrandom('hello.');

@dmnsgn dmnsgn mentioned this issue Mar 7, 2022
Merged
11 tasks
@vorg
Copy link
Member Author

vorg commented Mar 7, 2022

Note: calling Math.seedrandom('constant') without new will make Math.random() predictable globally, which is intended to be useful for derandomizing code for testing, but should not be done in a production library. If you need a local seeded PRNG, use myrng = new Math.seedrandom('seed') instead.

https://www.npmjs.com/package/seedrandom

to keep api consistent we could create our own global random generator seeded with eg. Date.now().

This will prevent polluting Math.random() but will still cause leaks between Nodes. To fix that we could provide constructor e.g.

import { randomFloat, create } from `pex-random`

const rndLocalNoSeed = create()
const rndLocalSeed = create("randomstring")

//0..1 using "global" random generator
randomFloat() 

//0..1 using "local" random generator using global seed
rndLocalSeed.randomFloat()

//0..1 using "local" random generator using local seed
rndLocalSeed.randomFloat()

The difference between those to would be that if other pieces of your app code / nodes use global random generator they can reset it by calling global randomSeed() and having local generator prevents that.

Not providing a seed in create() would "inherit" current seed from global generator but then diverge from it independently after use. Other local generators that also use initial global seed would start from the same point and could produce same sequence on numbers. To avoid that we could use Date.now() or global seed + some global counter tracking how many custom random generators have been created.

@dmnsgn dmnsgn closed this as completed in 6b5ea1f Feb 5, 2024
@dmnsgn dmnsgn closed this as completed in #5 Feb 5, 2024
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 a pull request may close this issue.

1 participant