Functions - JavaScript Random
Posted by Superadmin on May 06 2023 02:37:15

JavaScript Random

By Anusua DuttaAnusua Dutta
  

JavaScript Random

Definition of JavaScript Random

JavaScript random method is part of JavaScript supporting ECMAScript 1 version which returns a random number ranging from value 0 to 1. The returned number as part of the random method includes the value 0 but excludes the value 1. The range of returned numbers should be proper without much manipulation and is used by the math.random method. The random method doesn’t include any of the parameters which mean parameters are not passed as part of the JavaScript random method. The random method of JavaScript is suitable and is compatible with almost all types of browsers without much hindrance.

Syntax:

Math.random()

The syntax flow is in a way where the JavaScript random() method needs to find the number lying between the range and value from 0 to 1 and then the ability to manipulate other values also makes it work in a way where the function has the ability to generate a random number which can be used for creating a lot of application instantly.

How does Random Method Work in JavaScript?

the random method in JavaScript is a special method that is used for generating a random number that will be used at some point in time for all the manipulations and calculations related to random integer purposes. The random integers just generated are used for returning random numbers which will get further passed using parameters in JavaScript.

Let’s see the actual working flow for JavaScript which is as follows:

Examples of JavaScript Random

Following are the examples are given below:

Example #1

This program demonstrates random number generation using math.random method and math.floor method followed by the manipulation according to the requirement for generating the random number as shown in the output.

Code:

<!DOCTYPE html>
<html>
<body>
<p>Press the button for number 0 to 9 to trace for random number.</p>
<button onclick="a_functn()">Run</button>
<p id="demo_1"></p>
<script>
function a_functn() {
var z_i = Math.floor((Math.random() /9) + 3);
document.getElementById("demo_1").innerHTML = z_i;
}
</script>
</body>
</html>

Output:

JavaScript Random-1.1

Example #2

This program demonstrates random number generation using math.random method and math.floor method followed by the manipulation according to the requirement for generating the random number as shown in the output.

Code:

<!DOCTYPE html>
<html>
<body>
<p>Press the button for number to trace for 1 to 10 random number.</p>
<button onclick="k_functn()">Run</button>
<p id="demo_2"></p>
<script>
function k_functn() {
var y_u = Math.floor((Math.random() * 12) - 6);
document.getElementById("demo_2").innerHTML = y_u
}
</script>
</body>
</html>

Output:

JavaScript Random-1.2

Example #3

This program demonstrates random number generation using math.random method and math.floor method followed by the manipulation according to the requirement for generating the random number as shown in the output.

Code:

<!DOCTYPE html>
<html>
<body>
<p>Press the button for number to trace for generating random number.</p>
<button onclick="m_functn()">Run</button>
<p id="demo_4"></p>
<script>
function m_functn() {
var zo_1 = Math.floor((Math.random() * 100) + 6);
document.getElementById("demo_4").innerHTML = zo_1
}
</script>
</body>
</html>

Output:

Example-1.3

Example #4

This program demonstrates random number generation using math.random method and math.floor method followed by the manipulation according to the requirement for generating the random number as shown in the output.

Code:

<!DOCTYPE html>
<html>
<body>
<p>Press the button for number to trace for generating random number.</p>
<button onclick="o_functn()">Run</button>
<p id="demo_4"></p>
<script>
function o_functn() {
var ho_1 = Math.floor((Math.random() * 50) + 3);
document.getElementById("demo_4").innerHTML = ho_1
}
</script>
</body>
</html>

Output:

Example -1.4

Note: On all the above examples it is true that clicking on the run button generates various values ranging from 0 to 10 or anything which is specified all the values or number generated is varied and can be different which can be seen after clicking on run many times.

Advantages

Conclusion

The javaScript random() method is a very versatile and flexible method that is liked by programmers when it comes for the generation of random numbers and playing around with it, as it reduces the boilerplate or the complexity faced by the programmers at the time of implementation of application including random numbers.