In this article, we will delve into the topic of generating random numbers in JavaScript. To begin with, it's essential to familiarize ourselves with the Math object.
What is a Math object?
JavaScript includes a predefined static object known as "Math".
This entity allows you to carry out mathematical computations. It is equipped with various pre-defined methods that facilitate the execution of mathematical tasks.
Syntax:
Math.method_name(number);
In JavaScript, we can generate a random number by utilizing the following methods:
- Math.random
- Math.floor
We will thoroughly understand these techniques through the use of demonstrations.
Math.random
This technique is employed to generate a random value ranging from 0 to 1, indicating that it consistently produces a number that is less than 1.
Syntax:
const num = Math.random();
Demonstrations:
We will examine the examples that illustrate how to create a random number using the Math.random function.
Demo 1:
We will generate a random decimal value ranging from 0 to 1 by employing the Math.random function.
Code:
const num = Math.random();
console.log(num);
Output:
In the output displayed below, we observe a decimal random number ranging from 0 to 1.
0.644745759039999
Demo 2:
In this demonstration, we will generate a random integer by employing Math.random.
Code:
const num = Math.random() * 2.5;
console.log(`${num} is a random whole number.`);
Output:
We can observe the result, which is a randomly generated integer.
2.1084120100254884 is a random whole number.
Demo 3:
We will generate a random number within a defined range by employing the Math.random function.
Code:
const getRandomNumber = (min, max) => {
return Math.random() * (max - min) + min;
}
// Random number between 5 and 10
const random_num = getRandomNumber(5, 10);
console.log(random_num);
// Random number between 1 and 50
const random_num2 = getRandomNumber(1, 50);
console.log(random_num2);
Output:
The output demonstrates that random numbers are produced within defined intervals of 5 to 10 and 1 to 50.
7.560610340629418
18.87035185975946
Demo 4:
We will obtain a random integer value within a specified range by using the Math.random function.
Code:
const getRandomInteger = (min, max) => {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
// Random integer between 8 and 12
const randomInteger = getRandomInteger(8, 12);
console.log(randomInteger);
// Random integer between 15 and 75
const randomInteger2 = getRandomInteger(15, 75);
console.log(randomInteger2);
Output:
We can observe the result that comprises a random integer value falling within designated ranges.
Demo 5:
We will create a random number that includes the designated number of decimal places by employing the Math.random function.
Code:
function randomNumber (min, max, places) {
const number1 = (Math.random() * (max - min + 1)) + min;
return Number.parseFloat(number1).toFixed(places);
}
const number2 = randomNumber(4, 12, 2);
console.log(number2);
Output:
We can observe the result, which is a randomly generated number featuring two digits after the decimal point.
Math.floor
This function is utilized to truncate a number to the closest lower integer. It produces a random value that falls within the range of 0 to 1, indicating that the resulting number is consistently below 1. By combining Math.floor with Math.random, we can create a random integer.
Demo 1:
We will generate a random integer ranging from 0 to 10 without any decimal component by employing the Math.floor method.
Code:
const num = Math.floor(Math.random() * (10 - 1)) + 1;
console.log(`Random value between 0 and 10 is ${num}.`);
Output:
We can observe the result, which is a random integer ranging from 0 to 10, exclusive of any decimal points.
Random value between 0 and 10 is 4.
Demo 2:
We will generate a whole number that falls within the range specified by the two numbers provided by the users.
Code:
// input from the user
const min = parseInt(prompt("Enter a mininum value: "));
const max = parseInt(prompt("Enter a maximum value: "));
// getting a random number
const num = Math.floor(Math.random() * (max - min + 1)) + min;
// displaying a random number
console.log(`Random value between ${min} and ${max} is ${num}.`);
Output:
We can observe the output that displays a random integer without any decimal points, generated between the two numbers provided by the users.
Enter a mininum value: 60
Enter a maximum value: 105
Random value between 60 and 105 is 72.
Demo 4:
We will generate inclusive random numbers within a defined range by employing the Math.ceil, Math.floor, and Math.random functions.
Code:
const getRandomIntegerInclusive = (min, max) => {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// Random integer between 6 and 10, inclusive
const randomInteger1 = getRandomIntegerInclusive(6, 10);
console.log(randomInteger1);
// Random integer between 2 and 60, inclusive
const randomInteger2 = getRandomIntegerInclusive(2, 60);
console.log(randomInteger2);
Output:
We can observe the result, which is a random integer that falls between two specified numbers without any decimal points.
Conclusion:
In this article, we have explored the concept of generating random numbers in JavaScript. We have examined how to create a random number through various demonstrations. To achieve this, we can employ the Math.random and Math.floor functions.