Home > Web Front-end > JS Tutorial > Random numbers in javascript math random How to generate random numbers within a specified range of values ​​_javascript skills

Random numbers in javascript math random How to generate random numbers within a specified range of values ​​_javascript skills

WBOY
Release: 2016-05-16 15:35:53
Original
2094 people have browsed it

Today a friend asked me for advice: How to generate random numbers in a specified range using JavaScript. I believe everyone knows that Math.random() is a method used to generate random numbers. However, the general reference manual does not explain how to use this method to generate random numbers within a specified range. This time I will introduce Math.random() in detail and how to use it to generate random numbers within a specified range.

w3school’s random() tutorial

Definition and usage

The random() method returns a random number between 0 ~ 1.

Grammar

Math.random()

Return value

A pseudo-random number between 0.0 ~ 1.0.

Example

In this example, we will get a random number between 0 and 1:

<script type="text/javascript">
document.write(Math.random());
</script>
// 输出:
0.15246391076246546
Copy after login

How to generate random numbers within a specified range

After reading the w3school tutorial, you should know the basic usage of the Math.random() method.

Use parseInt(), Math.floor() or Math.ceil() for rounding

We see that using the Math.random() method directly generates a number less than 1, so:

Math.random()*5

The result obtained is a random number less than 5. What we usually hope to get is an integer between 0-5, so we need to round the result to get the integer we expect. parseInt(), Math.floor() and Math.ceil() can all play a rounding role.

var randomNum = Math.random()*5;
alert(randomNum); // 2.9045290905811183 
alert(parseInt(randomNum,10)); // 2
alert(Math.floor(randomNum)); // 2
alert(Math.ceil(randomNum)); // 3
Copy after login

We can see from the tested code that parseInt() and Math.floor() have the same effect, both taking the integer part downwards. So parseInt(Math.random()*5,10) and Math.floor(Math.random()*5) are both generated random numbers between 0-4, Math.ceil(Math.random()*5 ) is a generated random number between 1-5.
Generate random numbers within a specified range

So, if you wish to generate a random number from 1 to any value, the formula is this:

// max - 期望的最大值
parseInt(Math.random()*max,10)+1;
Math.floor(Math.random()*max)+1;
Math.ceil(Math.random()*max);
Copy after login

If you want to generate a random number from 0 to any value, the formula is like this:

// max - 期望的最大值
parseInt(Math.random()*(max+1),10);
Math.floor(Math.random()*(max+1));
如果你希望生成任意值到任意值的随机数,公式就是这样的:
// max - 期望的最大值
// min - 期望的最小值 
parseInt(Math.random()*(max-min+1)+min,10);
Math.floor(Math.random()*(max-min+1)+min);
Copy after login

Have you learned a lot after reading this article? Do you understand the use of random numbers math random? I hope this article can help you, thank you!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template