JS Example for Beginners: Javascript Program to Generate a Random Number

(JavaScript programming Example for Beginners)

Javascript Program to Generate a Random Number

In this example, you will learn to generate a random number in JavaScript.

 


In JavaScript, you can generate a random number with the Math.random() function.

Math.random() returns a random floating-point number ranging from 0 to less than 1 (inclusive of 0 and exclusive of 1)


Example 1: Generate a Random Number

// generating  a random number
let a = Math.random();
console.log(a);

Output

0.5856407221615856

Here, we have declared a variable a and assigned it a random number greater than or equal to 0 and less than 1.

Note: You might get a different output in the above program as Math.random() will generate a random number.


We can use this value in the range (0,1) to find the random value between any two numbers using formula:

Math.random() * (highestNumber - lowestNumber) + lowestNumber

Example 2: Get a Random Number between 1 and 10

// generating a random number
let a = Math.random() * (10-1) + 1
console.log(`Random value between 1 and 10 is ${a}`);

Output

Random value between 1 and 10 is 7.392579122270686

This will show a random floating-point number greater than 1 and less than 10.


All the above examples give floating-point random numbers.

You can use Math.floor() to get a random integer value. Math.floor() returns the number by decreasing the value to the nearest integer value. For example,

Math.floor(5.389); // 5
Math.floor(5.9); // 5

The syntax to find the random integer value between two numbers:

Math.floor(Math.random() * (highestNumber - lowestNumber)) + lowestNumber

Example 3: Integer value between 1 and 10

// generating a random number
let a = Math.floor(Math.random() * (10 - 1)) + 1;
console.log(`Random value between 1 and 10 is ${a}`);

Output

Random value between 1 and 10 is 2

This will show integer output between 1(inclusive) to 10(exclusive), i.e.(1 to 9). Here, Math.floor() is used to convert decimal to integer value.


Similarly, if you want to find the random integer in between min(inclusive) to max(inclusive), you can use the following formula:

Math.floor(Math.random() * (max - min + 1)) + min

Example 4: Integer value between two numbers(inclusive)

// input from the user
let min = parseInt(prompt("Enter a min value: "));
let max = parseInt(prompt("Enter a max value: "));

// generating a random number
let a = Math.floor(Math.random() * (max - min + 1)) + min;

// display a random number
console.log(`Random value between ${min} and ${max} is ${a}`);

Output

Enter a min value: 1
Enter a max value: 50
Random value between 1 and 50 is 47

This will show the integer output between min(inclusive) to max(inclusive).

 

JS Example for Beginners: Javascript Program to Generate a Random Number

Sign up to get end-to-end “Learn By Coding” example.



Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.