JavaScript Random

    In the previous tutorial, we learned about JavaScript Math objects, there we also learned about random() method that can generate random numbers. In this tutorial, we will discuss the Math random method in detail because it is one of the most important and widely used JS Math methods.


    The Math.random() method can generate a random number between 0 and 1, where 0 is inclusive and 1 is exclusive. This means the random() method always returns a value equal to and greater than 0 and less than 1.


        random_number = Math.random();  //random number between 0 to 0.999999 
        console.log(random_number);   //0.717273747683947

    Generate Random Integers in JavaScript

    The Math.random() number always returns a decimal point number between 0 to 1(exclusive). But we can multiply the returned random number with an integer value to increase the range of the random number. And then roundup the number using either Math.floor() , Math.ceil() , Math.round() or Math.trunc() methods to get an integer value.


        //random number between 0 to 9
        random_number1 = Math.floor(Math.random()*10);  // round down the number 
        //random number between 0 to 9
        random_number2 = Math.ceil(Math.random()*10);  // round down up number 
        //random number between 0 to 9
        random_number3 = Math.trunc(Math.random()*10);  // remove the decimal points
        //random number between 0 to 9
        random_number4 = Math.round(Math.random()*10);  // round number 



    Create a Random function in JavaScript

    By far we have learned how to generate random numbers in JavaScript, and how to increase its range and generate random integer numbers. But by doing so we are generating only random numbers between o to n-1 . Let's say we want to create an OTP application where we want to generate a random 4 digit number, how can we do that. To achieve this objective we need to define a function or logic that can generate a random number between 1000 to 9999, that covers all the 4 digits random numbers.


        function generate_random_num(min_val, max_val)
            return Math.round(Math.random()*(max_val-min_val)) +min_val
        //four digit random number 
        rand_4 = generate_random_num(1000, 10000);
        //five digit random number 10000, 99999
        rand_5 = generate_random_num(10000, 100000);
        console.log("The four digit random number is: "+rand_4);
        console.log("The five digit random number is: "+rand_5);


    The four digit random number is: 4758
    The five digit random number is: 73767

    Behind the code

    In the above example the Math.round(Math.random()*(max_val-min_val)) +min_val statement will make sure that the random number always generated between min_vlaue (inclusive) and max_value (exclusive) . If you want to make both min_val and max_val inclusive, there you can use this return statement Math.round(Math.random()*(max_val-min_val+1)) +min_val


    • The Math.random() method returns a random number between 0(included) to 1(excluded).
    • To increase the range of generated random numbers we can multiply the generated number with a max value.
    • To generate a Integer random number we can round the generated random number using round(), floor(), ceil() or trunc() method.

    People are also reading: