Home  >  Article  >  Web Front-end  >  How to randomly appear a number in JavaScript

How to randomly appear a number in JavaScript

PHPz
PHPzOriginal
2023-04-25 17:31:01143browse

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.

  1. Math.round()

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.

  1. Math.floor()

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.

  1. Math.ceil()

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.

  1. Generate decimals between 0 and 1

As mentioned before, using Math.random() can quickly generate decimals between 0 and 1:

<code class="javascript">var n = Math.random(); //生成0至1之间的小数</code>
  1. Generate decimals within the specified range

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.

  1. Generate a random number with a specified number of decimal places

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn