javascript generates a random number method summary
- 2020-10-23 20:53:55
- OfStack
Today, another netizen asked me how JavaScript generates random Numbers in a specified range. Math. random() is a method that generates random Numbers, as we all know. However, the 1-like reference manual does not explain how to use this method to generate random Numbers within a specified range. This time I'll take a closer look at Math.random () and how you can use it to generate random Numbers within a specified range.
See the basic tutorial here
https://www.ofstack.com/w3school/js/jsref_random.htm
By the end of the tutorial, you should know the basic usage of the Math.random () method.
parseInt(), Math. floor(), or Math. ceil() are used for rounding off 5
We can see that the method Math. random() is directly used to generate a number less than 1, so:
Math.random()*5
The result is a random number less than 5. And what we usually want to get is an integer between 0 and 5, so we need to round off the result 4 into 5 to get the desired integer. 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
From the test code, we can see that parseInt() and Math.floor () have the same effect, both taking the integer part downward. Therefore, parseInt(Math.random ()*5,10) and ES46en.floor (Math.random ()*5) are generated random Numbers between 0 and 4, while Math.ceil (Math.random ()*5) are generated random Numbers between 1 and 5.
Generates a random number with a specified range of values
So, if you want to generate a random number from 1 to any value, the formula looks like this:
// max - Expected maximum
parseInt(Math.random()*max,10)+1;
Math.floor(Math.random()*max)+1;
Math.ceil(Math.random()*max);
If you want to generate a random number from 0 to any value, the formula looks like this:
// max - Expected maximum
parseInt(Math.random()*(max+1),10);
Math.floor(Math.random()*(max+1));
If you want to generate random Numbers from any value to any value, the formula looks like this:
// max - Expected maximum
// min - The desired minimum
parseInt(Math.random()*(max-min+1)+min,10);
Math.floor(Math.random()*(max-min+1)+min);
Let's take a look at some of the other ways that javascript generates random Numbers
1. Use built-in random number generation methods :(just described, here is a brief description)
Math.random(); // This method produces 1 a 0 to 1 Floating point number between.
Math.floor(Math.random()*10+1); //1-10
Math.floor(Math.random()*24);//0-23
2. Based on time, random Numbers can also be generated:
var now=new Date();
var number = now.getSeconds(); // This will produce 1 Based on the current time 0 to 59 The integer.
var now=new Date();
var number = now.getSeconds()%43; // This will produce 1 Based on the current time 0 to 42 The integer.
3.1 An excellent random number generator program that can be used in many fields.
<script language="JavaScript"><!--
// The Central Randomizer 1.3 (C) 1997 by Paul Houle (houle@msc.cornell.edu)
// See: http://www.msc.cornell.edu/~houle/javascript/randomizer.html
rnd.today=new Date();
rnd.seed=rnd.today.getTime();
function rnd() {
rnd.seed = (rnd.seed*9301+49297) % 233280;
return rnd.seed/(233280.0);
};
function rand(number) {
return Math.ceil(rnd()*number);
};
// end central randomizer. -->
</script>
Let's look at two more concrete examples,
The first method is realized by rewriting ES84en.random method, and the second method is changed from one C implementation, both of which can achieve the programming purpose.
Example 1:
<script language="javascript">
var native_random = Math.random;
Math.random = function(min, max, exact) {
if (arguments.length === 0)
{
return native_random();
}
else if (arguments.length === 1)
{
max = min;
min = 0;
}
var range = min + (native_random()*(max - min));
return exact === void(0) ? Math.round(range) : range.toFixed(exact);
};
document.write(Math.random());
document.write('<br />');
document.write(Math.random(10));
document.write('<br />');
document.write(Math.random(3,10));
document.write('<br />');
document.write(Math.random(2,10,4));
</script>
Example 2:
<script type="text/javascript">
var random = (function(){
var high = 1, low = 1 ^ 0x49616E42;
var shuffle = function(seed){
high = seed;
low = seed ^ 0x49616E42;
}
return function(){
var a = new Date()-0
shuffle(a);
high = (high << 16) + (high >> 16);
high += low;
low += high;
return high;
}
})();
alert( random() );
</script>
Ok, through these examples, you should have a corresponding understanding of javascript generated random Numbers, I hope this article can give you 1 inspiration.