javascript random number

Random Number Generator Javascript

In this tutorial, we will learn how to generate random numbers in javascript and we use a special method in javascript i.e.The Math.random() static method returns a floating-point, pseudo-random number that’s greater than or equal to 0 and less than 1, with approximately uniform distribution over that range β€” which you can then scale to your desired range. The implementation selects the initial seed to the random number generation algorithm; it cannot be chosen or reset by the user.

Math random() Syntax in JS

Math.random();

In Javascript random number simple syntax would be –

Math.random() Example in JavaScript

Example 1 :

const a = Math.random();

console.log(a);  // generating a random number

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.

Example 2 :

How to get a javascript Random Number between 1 and 10.

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

Output :

A random value between 1 and 10 is 1.1488764328463117

Example 3 :

How to get a random value between the range of 1 to 10.

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

Output :

Random value between 1 and 10 is 5

For JavaScript random number, we used the Math.random() function. This method returns the value between 0 (inclusive) and 1 (exclusive).

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.

Define a function to generate random numbers

From the above concept, we can create a function that takes two arguments, the highest and lowest to generate a random number between them and returns it.

Below is the code to define a function to return a random number from a given range :

function generateRandomNumber(lowest, highest) {    
let randomNumber = Math.random() * (highest - lowest) + lowest;
randomNumber = Math.floor(randomNumber); 
return randomNumber;
}

These functions can be called whenever there is a requirement of generating a random number, it is better to create a function instead of repeating the whole processor, again and again, to generate random numbers in different blocks.

Calling the above function :

const randomNumber = generateRandomNumber(1, 1000); 
console.log(randomNumber);

Output :

345

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);

Example :

function randomNumber(minimum, maximum) {
return Math.floor(Math.random() * (maximum - minimum) + minimum);
}
document.write("Generating a random number between the range 50 and 500: ")
// Here, we are calling the JavaScript function 
document.write('<strong>' + randomNumber(50, 500) + '</strong>');

Output :

Generating a random number between the range 50 and 500: 494

Math.random();

Example :

<html>
<head>
<title>Developer Helps | Javascript Random Number</title>
</head>
<body>
<h2>Javascript Random Number</h2>

Random Number between 0 and 1: <p id="random-number"></p>

<script>
document.getElementById("random-number").innerHTML = Math.random();
</script>

</body>
</html>

Output :

Javascript Random Number

Random Number between 0 and 1:
0.2535062639322767

JavaScript Random Integers

This example returns the random integer from 0 to 9.

Example :

<html>
<head>
<title>Developer Helps | Javascript Random Number</title>
</head>
<body>
<h2>Javascript Random Number</h2>

Random Integer between 0 and 9: <p id="random-number"></p>

<script>
document.getElementById("random-number").innerHTML = Math.floor(Math.random() * 10);
</script>

</body>
</html>

Output :

Javascript Random Number

Random Integer between 0 and 9:
2

JS Random Function

This JavaScript function returns a random number between min (included) and max (excluded).

Function :

function fetchRandomInteger(min, max) 
{
  return Math.floor(Math.random() * (max - min)) + min;
}

Example :

<html>
<head>
<title>Developer Helps | Javascript Random Number</title>
</head>
<body>
<h2>Javascript Random Number</h2>
<button onclick="fetchRandomInteger(0,10)">Try Click</button><br><br>
Random Function between min and max: <p id="random-number"></p>

<script>
function fetchRandomInteger(min, max) 
{
  document.getElementById("random-number").innerHTML = Math.floor(Math.random() * (max - min)) + min;
}
</script>

</body>
</html>

Output :

Javascript Random Number

Try Click

Random Function between min and max:
8

Thanks for the reading post. I hope you like and understand the post. If you have any doubts regarding this post please comment below.

More Related Post

Leave a comment

Your email address will not be published. Required fields are marked *