Math Random in Javascript: generating random numbers
Javascript provides a vast amount of math-related functions. In this article, we will explain how to use Math Random in Javascript. These in-built functions and objects are very useful while programming and developing. In-built functions and objects reduce the work for the developers. Some of the most commonly used in-built javascript functions and objects are parseFloat, parseInt, math, String, Number, etc. In-built objects in javascript have different methods and properties which extend their working in different variations. In this article, we will learn about one such object known as math, and its method, random.
The math random method
Math object provides various properties and methods. Random is one of the methods of math objects in javascript. We can use the random function to get a random number between 0 (inclusive) and 1 (exclusive). The result we get is a floating-point number which can include 0, but not 1. The random function is a useful function while developing such apps that require a random number to a decision.
The following examples show a random function used to generate a random floating-point number.
var result1 = Math.random(); var result2 = Math.random(); var reuslt3 = Math.random(); //result1 = 0.38628229857168694 //result2 = 0.4474997248824282 //result3 = 0.14050962942617873
The output is:
The result is different every time. We always get a different floating-point number than the previous one. The random function can be used to make random decisions also. Let’s discuss random decision making using a random function.
var number = Math.random(); var result; if(number <= 0.5) { result = 1; } else{ result = 0; }
Observe the above code. We used a variable named as the number and gave it any random number using Math.random function. In the if condition, we compared the number variable with 0.5. If it is less than or equal to 0.5, then the result is equal to 1 or else, it equals 0. This is random decision making.
Random function with a range
The random function of the math object by default returns a floating-point number between the range 0 (inclusive) and 1 (exclusive), but with certain different modifications using other functions, we can also extend the range. This can be done in two ways. Let’s discuss these ways of using the random function with range.
The range between 0 and a maximum value
This is one easy way of using math’s random function. We can get a random number between 0 and a specified maximum value by little modification.
var result1 = Math.random() * 10; var result2 = Math.random() * 10; var reuslt3 = Math.random() * 10; //result1 = 1.9409498473136177 //result2 = 3.6785094083002634 //result3 = 0.04600099618988773
The output is:
In the above example, we simply multiplied the returned value of the random function with 10. This will create a random number within the range, 0 and 10. We can use any number in the place of 10 and multiplying it with the random function will give a random number between 0 and that specified number.
The range between a minimum and maximum value
We can also get a random number between two specified numbers. There is a special logic for this. The following example shows how to get a random number between a minimum and maximum value.
var min = 50; var max = 100; var result1 = Math.random()*(max - min)+min; var result2 = Math.random()*(max - min)+min; var result3 = Math.random()*(max - min)+min; //result1 = 64.41960433466932 //result2 = 91.59727835073954 //result3 = 76.15457234855353
The output is:
In the above example, we declared two variables, min, and max, with values 50 and 100 respectively. To get a random number between these two values, first, we have to take the difference of both values and add it to the minimum value. The sum is then multiplied with the return value of the random function. The random number generated is always between the range we specified.
Javascript Math Randon with Integer numbers
We can see that the returned random number is always a floating-point number. What if we want an integer or whole number? We can use math object’s two other functions for this, ceil and floor. Let understand these two functions and their use with the help of an example.
var min = 50; var max = 100; var result1 = Math.ceil(Math.random()*(max - min)+min); var result2 = Math.floor(Math.random()*(max - min)+min); //result1 = 95 //result2 = 62
The output is:
In the above example, we used the random function to generate random numbers. But the output is not a floating-point number. This is because we passed the random function to the math’s ceil and floor function. The value returned from both the function is an integer. The ceil function round-offs the floating-point number upwards to the nearest integer while the floor function round-offs it downwards to the nearest integer.
Math random function into an array
We can also generate an array of random numbers using the random function in javascript. We need to generate random numbers in a for loop to create the array. The following code shows how to create an array of random numbers using the math random Javascript function.
var result; var arr = []; for(var i=0;i<5;i++) { result = Math.random(); arr.push(result) } // arr = [0.7493301502756253,0.18946813002946805,0.9884781177829836,0.5870931575914757,0.9730439867672014]
The output is:
In the above code, we used the random function inside the for loop to generate five random numbers. Then, we used the push method to add every value generated to the array. This how we create an array of random numbers.
We created an array of floating-numbers, we can also create an array containing random numbers between any range. The following code shows how to do this.
var arr = []; var result; var min = 50; var max = 100; for(var i=0;i<5;i++) { result = Math.ceil(Math.random() * (max-min) + min); arr.push(result) } // arr = [60,96,99,98,82]
The output is:
In the above code, we used the maximum and minimum values to generate random numbers and then used the ceil function to convert the floating-point numbers to integers.
Conclusion
Javascript is a vast language that comes with many in-built functions and objects. One of these objects is math that has many properties and methods. One of these methods, the math random Javascript function, we discussed in this article with examples. The random function is helpful during programming and development in many ways such as random decision making, random array generation, identification code generation, etc. The random function is easy to learn and use. Using it with other methods of math object, we can extend its working to another useful level.