Summary of JavaScript random numbers and VBScript (ASP) random numbers attached to advanced applications

Source: Internet
Author: User

1. Javascript random number function math. Random ()
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

The following is an application:
[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]

Javascript random number advanced application
① 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 And can be used in many fields. CopyCode The Code is as follows: <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>

Ii. VBScript (ASP) Random Number
Vbs Random Number Generation
A random number with no specified rangeCopy codeThe Code is as follows: function getrandomize (numstr)
Randomize
Getrandomize = int (numstr * RND) + 1)
End Function

Generate random numbers within a specified rangeCopy codeThe Code is as follows: function myrnd (nummin, nummax)
Randomize
Myrnd = int (RND * (nummax-nummin + 1) + nummin
End Function

Below are some examples of JS random numbers.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>

DEMO code:
[Ctrl + A select all Note: To introduce external JS, refresh it before execution]

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.