The random() function must be called through the substitute object ‘math’ because it is the stable function of the math object.
The Math.random() function returns the pseudo-random numbers, floating points between the range of 0 and 1 (including 0, not 1).
Math.random() method is used to return the integer in between the minimum and maximum value.
Example 1: How to find random numbers within the default range
The Math.random() method finds the random number within the default range. This method returns a random number that includes the starting integer but not the ending.
This example shows how to find the random number using this function.
Here in this example, a random() method is called using a math class.
The output showed the returned random number automatically in the range of 0 and 1 (>=0 & < 1).
Example 2: How to find a random number between specified values
The output showed the randomly generated integer between the two values. The output value is ‘1’ which is greater than the minimum values but less than the maximum value in the given input.
Example 3: How to find random numbers between the min and max values
Math.random() function searches an integer in between the minimum and maximum values within a specified range. Besides the minimum values, it also includes the maximum values.
In this example, two random values 17 and 64 are given to find the new random integer between the maximum and minimum values. The Math.random() function executes to search the random integer in between the minimum and maximum values.
The output showed that ‘44’ is the random number generated between ‘17’ and ‘64’.