Generate a Random number using JavaScript!
Random number generator function in JavaScript
Math.random()-
This is simply a function that generates a random floating-point number when it is been called.
So by default, its value ranges from (0 - 0.99999.....) it never reaches to one.
And the most unique thing is that it generates different value every single time . There is no pattern of the values generated.
Math.floor()-
This function rounds down a number to the next smallest integer or in other words, we can say that with the help of this function, it converts the float numbers into integers.
Example-
var n=Math.random();
console.log(n);
OUTPUT - 0.954137, 0.25469, 0.6547135 etc.
Example-
var n=Math.random();
n=n*6; // while multiplying by 6 it will now produce random (0.1256 - 5.14236)
console.log(n);
OUTPUT - 1.4252, 4.31456, 5.34863 etc
Example-
var n=Math.random();
n=n*6;
n=Math.floor(n)+1; //adding 1 will give numbers from (1 - 6)
console.log(n);
OUTPUT - 1, 6, 4, 5, 2, 3 etc
Conclusion-
Understanding the working of Math.random() & Math.floor().