Math.random() generates a pseudorandom number—but what does that mean?

I believe it has something to do with the algorithm behind it. Could someone please explain in simple terms what pseudorandom means and why it’s not just called random?

as it is generated by an algorithm it is not really random. With enough numbers generated you would see a pattern. If you want to get just a number it could even be fine, but if you need random numbers for simulations it may not work as needed.

There are other ways to generate random numbers, it depends on what you need.

Khan Academy has a section in their natural simulations course about randomness (they use ProcessingJS, a library with which is possible to draw with JavaScript, so not everything would be familiar to you): https://www.khanacademy.org/computing/computer-programming/programming-natural-simulations#programming-randomness

2 Likes

Its designed to generate an approximately uniform distribution of numbers between 0 and 1. That’s generally what you want, but it isn’t really random. How random the randomness is depends on system. And the more random you make the software algorithm, the more resource-intensive it is. Nb actual randomness is almost impossible to do at a software level, you need hardware.

1 Like

Most pesudo random number generators are basically are a super complicated function. This function returns a float/double between 0 and 1 with uniform probability, but there is a pattern to how it finds the number to return.

This makes the function look random, but it isn’t truly random.

True random number generators (TRNG or HRNG) measure randomness from real world phenomenon and use that to return a truly random number that does not have any pattern associated with its generation.

This means that a pesudo random number generator should not be used for cryptographic purposes. Patterns help people break codes. But if you just need “random” inputs for testing or creating different behaviors in your code, a pesudo random number generator works great.

2 Likes