# Js random number code Daquan page 1/2

Source: Internet
Author: User

Js random number base
1. javascript random number function Math. random ()

Random (Math. random Method)
Public static random (): Number

Returns a pseudo-random number n, where 0 <=n <1. The returned number is called "pseudo-random" because it is calculated technically in a confidential manner.

Return
Number: a Number.

Generate random numbers within a specified range
The Math. random () method has no parameters. 0 ~ is returned ~ Random number between 1. If 0 ~ is to be generated ~ Random between n
Number, which can be in the following format:

Math. floor (Math. random () * n)
To generate m ~ Random Number between n, which can be used:
Math. floor (Math. random () * n) + m

① Since JavaScript was generated, many browsers have built-in random number generation methods. For example:
Var number = Math. random ();
This method generates a floating point number between 0 and 1.
② Based on Time, random numbers can also be generated. For example:
Var now = new Date ();
Var number = now. getSeconds ();
This generates an integer ranging from 0 to 59 based on the current time.
Var now = new Date ();
Var number = now. getSeconds () % 43;
This generates an integer ranging from 0 to 42 based on the current time.
③ Here we will introduce a fairly good random number generator program, which can be applied 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>
Detailed source reference: http://www.jb51.net/article/12566.htm

So how can we easily obtain a random number within a specified range? After my comparison, My function is the most concise and practical drop on the Internet so far.
Instance code:

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Code Description:
Copy codeThe Code is as follows:
<Script>
// Declare a random number variable. The default value is 1.
Var GetRandomn = 1;
// Obtain the function of the value in the random range
Function GetRandom (n) {GetRandomn = Math. floor (Math. random () * n + 1 )}
// Start the call and obtain a random number ranging from 1.
GetRandom ("100 ");
// View output
Document. write (GetRandomn)
</Script>

Is the Code quite concise? GetRandomn is the random number variable and can be called at will.
Let's take a random display of the special effect code:
<Script>
// At the beginning, obtain a random number ranging from 1 to 3.
GetRandom ("3 ");
// Call a project that meets the random number
Switch (GetRandomn ){
Case 1:
Document. write ("when the random number is 1, the script home is displayed ")
Break;
Case 2:
Document. write ("www.jb51.net" is displayed when the random number is 2 ")
Break;
}
</Script>

Related Keywords:
Related Article

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.