Home > Article > Web Front-end > How to create random numbers in JavaScript
Random number is a very important function in JavaScript. It can not only be used to generate random alphanumeric characters or generate keys for encryption, but can also be used to create lottery games, simulation games and other applications. This article will detail how JavaScript creates random numbers.
Random number generation function in JavaScript
In JavaScript, we can use the random() method of the Math object to generate random numbers. This method will return a pseudo-random number between 0 and 1, inclusive, and 1 exclusive. The following is a simple example that generates a random number and outputs it in the console:
var randomNum = Math.random(); console.log(randomNum);
Since the Math.random() method returns a pseudo-random number, the random number generated each time the code is run is different. However, we may need to generate random numbers within a specified range, which requires processing the Math.random() method.
Generate random numbers within the specified range
We can generate random numbers within the specified range through the following methods:
The following is an example to generate a random integer between 0 and 9:
var randomInt = Math.floor(Math.random() * 10); console.log(randomInt);
The result at this time will be an integer between 0 and 9.
Similarly, we can generate random integers in other ranges by modifying the parameters. For example, generate a random integer between 10 and 20:
var randomInt = Math.floor(Math.random() * 11 + 10); console.log(randomInt);
Here, we change the value range of the result from 0~9 to 10~20, so the result is an integer between 10 and 20.
Generate random letters and numbers
In addition to generating random integers, we can also generate random letters and numbers. In order to achieve this function, we can define a string containing all possible characters. Then use a random number generator to select a character from it.
The following is an example to generate a random string containing uppercase and lowercase letters and numbers:
var chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; var randomStr = ''; for(var i = 0; i < 8; i++) { randomStr += chars[Math.floor(Math.random() * chars.length)]; } console.log(randomStr);
Here, we selected a string composed of numbers, lowercase and uppercase letters as our random character source. First define an empty string randomStr, and then use a for loop to select random characters from the character source and add them to the randomStr string. After looping 8 times, we get a string containing 8 random characters.
Summary
The method of creating random numbers in JavaScript is very simple, and random numbers of different ranges and types can be generated as needed. When we need to make some applications that require random elements, we can use these methods to achieve our needs.
The above is the detailed content of How to create random numbers in JavaScript. For more information, please follow other related articles on the PHP Chinese website!