What is the difference between the way Math.random()
and crypto.randomInt()
work in node.js?
And if I will run them in a loop for a long time, is it possible for me to get the same result an amount of times that exceeds the statistics?
Asked
Active
Viewed 1,414 times
1 Answers
3
Math.random() uses a basic psuedo-random algorithm that is not necessarily cryptographically secure.
The requirements for randomness for cryptographic purposes are generally much more stringent than what you would need if you just want to randomize some motion in an animation.
If you're using it for security then use the crypto version.

AminM
- 822
- 4
- 11
-
1I think it's also much slower – Raz Luvaton Sep 21 '21 at 18:14
-
See also: https://stackoverflow.com/questions/40565261/how-much-entropy-does-math-random-generate/53135895#53135895 ; https://stackoverflow.com/questions/53002023/when-does-math-random-start-repeating – Peter O. Sep 21 '21 at 19:08
-
Crypto is usually slower. – AminM Sep 21 '21 at 19:22