Use crypto.getRandomValues for new seeds
See original GitHub issueIt’s ill advised to use plain math.random
due to the low entropy available in testing environments. Instead, use getRandomValues where available (IE11+ var crypto = window.crypto || window.msCrypto;
).
Update: The concern I’m trying to address here is collisions that are the result of low-entropy testing environments, not deficits to the underlying PRNG. It’s an edge case, but the fix is trivial.
Issue Analytics
- State:
- Created 8 years ago
- Comments:12 (6 by maintainers)
Top Results From Across the Web
Crypto.getRandomValues()
getRandomValues () method lets you get cryptographically strong random values. The array given as the parameter is filled with random numbers ( ...
Read more >Crypto.getRandomValues() - Web APIs | MDN
getRandomValues () method lets you get cryptographically strong random values. The array given as the parameter is filled with random numbers ( ...
Read more >Crypto.getRandomValues() - Web APIs
getRandomValues () method lets you get cryptographically strong random values. ... Implementations are required to use a seed with enough entropy, ...
Read more >Shuffling a poker deck in JavaScript with window.crypto ...
Using window.crypto.getRandomValues we can generate the required 226 bits of entropy to be used as our seed. If that still isn't enough, we...
Read more >How to use getRandomValues function in Crypto - Tabnine
export function uuid() { return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, function(c) { return (c ^ window.crypto.getRandomValues(new ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This has come up multiple times and my answer is always the same.
First, Chance doesn’t use
Math.random()
under the hood to generate its random, by default it uses a Mersenne Twister. More info in the docsIt does use
Math.random()
by default to pick a seed for the twister if none is provided as the default. This is an important distinction.This was intentional to provide the ability for “repeatable random” if desired by providing the same seed. This is useful for certain use cases (like unit tests where you may want repeatable results).
Second, the rationale for not using any specific crypto library under the hood was that Chance was built to work almost anywhere. It will currently run in practically any Browser or Node.js, in Mongo, in Rhino. I’ve even had people email me about successfully cross-compiling Chance to Java and other languages.
I’ve ensured this is the case by having zero dependencies, using only vanilla JS (es5), and by not depending on any specific crypto implementations (e.g.
window.crypto
or Node’s crypto)If Chance instead used what you suggest (
window.crypto
) that limits functionality only to the browser (breaking all server-side implementations along with cross compilation), and further only to browsers that support crypto (which is admittedly becoming more prevalent but still not yet universal).However, I anticipated this from Chance’s humble start a few years back, so back in 2013 I wrote into Chance the ability to specify an arbitrary random function of your choosing so a developer could override the default and use any method for the random number generator underlying Chance. This provides optimal flexibility.
You can simply specify the function you’d like to use as a random number generator (any function which returns a number from 0 to 1) and Chance will use your method instead of its Mersenne Twister. See the docs here.
So if you’d like to specify
window.crypto
for your particular application rather than the Mersenne Twister, the following snippet will do it for you:This will get you the results you’d like without limiting compatibility and without dropping the feature of being able to provide a seed and get repeatable random.
Given that not using
window.crypto
is a feature, not a bug, I am closing this issue.Both are killer features, I have both in my homegrown solution, but these are orthogonal issues.
Low entropy testing environments are honestly one of the few places in which seeds with low entropy can be found. Sure, tests with side-effects are somewhat rare and users can work around the issue, but it would (as you pointed out three years ago) be nice if Chance handled this on its own.
I wrote the above code without much thought, here’s a better proposal that uses an enhanced version of underscore’s Node detection and produces a random integer all wrapped neatly in a try/catch.
Unless someone has deliberately sabotaged the environment in a very specific way, you will get a random seed.