Random Numbers
Let's look at how to create random numbers in JavaScript.
var randomNumber = Math.random();
console.log(randomNumber);
By only doing this, it will print out numbers between 0
and 1
:
Examples:
0.8813606250872746
0.05016415843877131
If you want below a certain number, then you can multiply the randomNumber
with the limit (in this example the range is 0
– 12
):
var randomNumber = Math.random() * 12;
console.log(randomNumber);
Examples:
11.553345329163783
3.468040622230529
Then, we can use floor
to round the randomNumber
downwards to the nearest integer:
var randomNumber = Math.floor(Math.random() * 12);
console.log(randomNumber);
Examples:
3
7