JavaScript generation Random number method summary _javascript skill

Source: Internet
Author: User
Tags current time shuffle

Today, another netizen asked me how JavaScript generates a specified range of numerical random numbers. Math.random () This method is believed to be known to generate random numbers. However, the general reference manual does not show how to use this method to generate random numbers within a specified range. This time I'll give you a detailed introduction to Math.random () and how to use it to generate random numbers within the scope of the development.

Basic tutorials See here

Http://www.jb51.net/w3school/js/jsref_random.htm

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

Rounding with parseint (), Math.floor () or Math.ceil ()

We see that the direct use of the Math.random () method produces a number less than 1, so:

Math.random () *5

The result is a random number less than 5. And what we usually want is an integer between 0-5, so we need to round up the resulting results and get the integers we expect. parseint (), Math.floor () and Math.ceil () can all be rounded up.

var randomnum = Math.random () *5;
alert (randomnum); 2.9045290905811183 
alert (parseint (randomnum,10));//2
alert (Math.floor (Randomnum));//2
alert ( Math.ceil (Randomnum)); 3

As we can see from the test code, the effect of the parseint () and Math.floor () is the same, and all the parts of the integer are taken down. So parseint (Math.random () *5,10) and Math.floor (Math.random () *5) are random numbers between 0-4 generated, Math.ceil (Math.random () *5) is the number of random numbers between 1-5 generated.

Generate a specified range of numeric random numbers

So, if you want to generate random numbers of 1 to any value, the formula is this:

Max-Desired Maximum value
parseint (Math.random () *max,10) +1;
Math.floor (Math.random () *max) +1;
Math.ceil (Math.random () *max);

If you want to generate random numbers of 0 to any value, the formula is this:

Max-expected maximum value
parseint (Math.random () * (max+1), ten);
Math.floor (Math.random () * (max+1));

If you want to generate random numbers of arbitrary values to any value, the formula is this:

Max-expected maximum
/min-Expected minimum value 
parseint (Math.random () * (max-min+1) +min,10);
Math.floor (Math.random () * (max-min+1) +min);

So let's look at some other ways that JavaScript generates random numbers.

1. Use the built-in random number occurrence method: (just said, here is a brief description)

Math.random (); This method produces a floating-point number between 0 and 1.
Math.floor (Math.random () *10+1);//1-10
Math.floor (Math.random () *24);//0-23 

2. Based on time, can also produce random numbers:

Copy Code code as follows:
var now=new Date ();
var number = Now.getseconds (); This will result in a 0 to 59 integer based on the current time.

var now=new Date ();
var number = Now.getseconds ()%43; This will result in a 0 to 42 integer based on the current time.

3. A fairly excellent random number generator program that can be applied in many fields.

<script language= "JavaScript" ><!--
//The Randomizer 1.3 (C) 1997 by Paul Houle (HOULE@MSC.CORNELL.E DU)
//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 of the randomizer. -->
</script>

Let's take a look at 2 more specific examples,

The first method is implemented by rewriting the Math.random method, and the second method is implemented from a C, which can achieve programming purposes.

Example one:

<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);
document.write (' <br/> ');
document.write (Math.random (3,10));
document.write (' <br/> ');
document.write (Math.random (2,10,4));
</script>

Example two:

<script type= "Text/javascript" >
var random = (function () {
	var = 1, low = 1 ^ 0x49616e42;
	var shuffle = function (seed) {high
		= seed;
		Low = seed ^ 0x49616e42;
	}
	
	return function () {
  	var a = new Date ()-0
   	Shuffle (a);
  	High = [High <<] + (high >>);
  	High + = low;
  		Low = = high;
   	return high;
 	}
) ();
 
Alert (random ());
</script>

Well, through these examples, we should have a corresponding understanding of JavaScript generated random numbers, I hope this article can give you some inspiration.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.