So the complexity actually comes from the requirements you give it.

But asking for a random integer is a common task. So it's handy to create function and keep it in your library. Don't go sprinkling Math.floor(Math.random() * 10) randomly around your codebase :D

P.S. I prefixed this with pseudo because there is no true Random in JavaScript. This is very important when it comes to cryptography. Google PRNG if you want to learn more.

## re: How do you do random? VIEW POST

FULL DISCUSSION`Math.random()`

produces a random`float`

(all JavaScript numbers are of type`float`

) from`0`

to`1`

.Adding the requirement of 0-10 means you have to multiply by 10 or

`* 10`

.The last requirement was the number must be an

`Integer`

, which is the`math.floor`

.Of course, if you only wanted a random float between

`0`

and`1`

, it would be just one call.And in PHP if you wanted a random float between 0 and 1, you would also have to do more work: Random Float between 0 and 1 in PHP

So the complexity actually comes from the requirements you give it.

But asking for a random integer is a common task. So it's handy to create function and keep it in your library. Don't go sprinkling

`Math.floor(Math.random() * 10)`

randomly around your codebase :DP.S. I prefixed this with

`pseudo`

because there is no true Random in JavaScript. This is very important when it comes to cryptography. Google`PRNG`

if you want to learn more.For a cryptographically strong random value, you might want to look into Crypto.getRandomValues()

Cheers!

I wish PM's understood this more.

There's a bug in your example; if the output is supposed to be

`[start, end)`

, then the correct implementation is:✌😉

I probably should have run it once. lol.

I gotta stop typing code directly into editors.

Good catch!