When users generate a number by a process whose result is unpredictable is known as Random Number. Using JavaScript, users can achieve this using the Math.random() function. We will study how to generate a random number using JavaScript in the article.
What are random whole numbers?
By the term itself, we can guess the meaning that random numbers are numbers chosen by chance. These numbers come with no predictions, and all the numbers in the number system will have equal probability when users choose any number. There are different functions in JavaScript that creates or generates random whole numbers. These are as follows:
Methods to generate random whole numbers using JavaScript:
Users can efficiently generate a random number falling within the range of two particular numbers instead of generating a random number falling within the range of 0 to any random specific number like the traditional approach.
Some functions like Math.random() function, Math.floor() function, etc. First, let us see how the Math.random() and Math.floor() functions work before generating random whole numbers.
Example 1: Using the Math.random() function to generate random numbers:
Programmers can use the Math.random() function to generate a floating-point pseudo-random number falling within the range [0,1) , 0 (inclusive) and 1 (exclusive). Programmers can scale the range of the random number according to the preferred range.
Syntax:
Math.random();
Note: This function does not accept any parameter.
Code Snippet:
function randomNumber(minimum, maximum) {
return Math.random() * (maximum - minimum) + minimum;
}
document.write("Generating a random Number from the range 5 to 10: ")
// Here, we are calling the JavaScript function
document.write( randomNumber(5, 10) );
Output:
Explanation:
We have generated a random number using the Math.random() function in floating point format, as we see in the output console. First, we have created a JS function that will return a random number in the output console. Then we called the Math.random() function.
Example 2: Using the Math.floor() function to generate random numbers:
Similar to the Math.random() function, the Math.floor() function will generate a random number, but this function rounds down the number to the nearest whole number.
Syntax:
Math.floor(x)
Code Snippet:
function randomNumber(minimum, maximum) {
return Math.floor(Math.random() * (maximum - minimum) + minimum);
}
document.write("Generating a random Number from the range 50 and 500: ")
// Here, we are calling the JavaScript function
document.write('<strong>' + randomNumber(50, 500) + '</strong>');
Output:
Explanation:
We have generated a random number using the Math.floor() function in whole number format, as we see in the output console. First, we have created a JS function that will return a random number in the output console.
Then we called the Math.floor() function. The function generates the number and rounds it to the nearest whole number.
Main Method: Using both Math.random() and Math.floor() functions to generate a random whole number:
It is the best technique to generate a random whole number using the two JavaScript functions, Math.random() and Math.floor(). These two functions work in combination to generate a random whole number.
It suggests programmers can generate a random number and round it down to the nearest whole number.
Syntax:
Math.floor(Math.random() * (maximum - minimum + 1) + minimum
Code Snippet:
var num;
var demo = (minimum, maximum) => num = Math.floor(Math.random() * (maximum - minimum + 1) + minimum);
demo(10, 100);
document.write("Generating a random Number from the range 10 and 100: " + num)
Output:
Explanation:
We created a function with two arguments, minimum and maximum. Then we passed the formula for getting a random whole number within a range, and the compiler will print the formula in the console. Lastly, we call the function.
Conclusion:
In this article, we have seen how to generate random whole numbers using the two functions of JavaScript, namely the Math.random() and Math.floor() functions. The combination of both JS functions will generate a pure random whole number.