Randomly generating numbers is one of the common requirements in JavaScript programming. In JavaScript, we can use Math objects to generate random numbers. Next, I'll show you how to use a Math object to randomly generate numbers.
Math.random()
Math.random() is JavaScript’s original method of generating random numbers. This method will return a random number between 0 and 1. For example:
<code class="javascript">var n = Math.random();</code>
The above code will randomly generate a number between 0 and 1 and save it in the variable n.
Generate integer random numbers
There are many ways to generate integer random numbers. Let’s look at a few examples.
The Math.round() method is the method used for rounding in the Math object. If we want to convert a random decimal to an integer, You can use this method. For example:
<code class="javascript">var n = Math.round(Math.random() * 10); //生成0至10之间的整数</code>
Of course, if we want to generate random integers in other intervals, we only need to change the multiplier and addend.
The Math.floor() method rounds down. This method returns the largest integer less than or equal to the given number. We can use this to generate integer random numbers within a specified range. For example:
<code class="javascript">var n = Math.floor(Math.random() * 10) + 1; //生成1至10之间的整数</code>
The above code will generate an integer between 1 and 10 and save it in the variable n.
The Math.ceil() method rounds up. This method returns the smallest integer greater than or equal to the given number. We can use this to generate integer random numbers within a specified range. For example:
<code class="javascript">var n = Math.ceil(Math.random() * 10); //生成1至10之间的整数</code>
The above code will generate an integer between 1 and 10 and save it in the variable n.
Randomly generate decimals within the specified range
Sometimes we need to generate decimals within the specified range. Here are a few examples.
As mentioned before, using Math.random() can quickly generate decimals between 0 and 1:
<code class="javascript">var n = Math.random(); //生成0至1之间的小数</code>
If we need to generate decimals within the specified range, such as between -1 and 1, we can use the following code:
<code class="javascript">var n = Math.random() * 2 - 1; //生成-1至1之间的小数</code>
The above code will generate a decimal between -1 and 1 and save it in the variable n.
If we need to generate a random number with a specified number of decimal places, such as generating a random number with two decimal places, you can use The following code:
<code class="javascript">var n = Math.floor(Math.random() * 100) / 100; //生成小数点后两位的随机数</code>
The above code will generate a random number with two decimal places and save it in the variable n.
So far, we have learned how to generate random numbers in JavaScript. Now we can practice it and implement random number generation in the code, bringing more possibilities and fun to the program.
The above is the detailed content of How to randomly appear a number in JavaScript. For more information, please follow other related articles on the PHP Chinese website!