JavaScript random number and advanced application with VBScript (ASP) random number summary _javascript tips

Source: Internet
Author: User
First, is the JavaScript random number function math.random ()
Generate random numbers for a specified range
The Math.random () method has no parameters and returns the number of random numbers between 0~1 if a random between 0~n is to be generated
Number, you can use the following format:

Math.floor (Math.random () *n)
To generate a random number between m~n, you can use the following:
Math.floor (Math.random () *n) +m

Here is the application:
<script language = "javascript" type = "Text/javascript" > <!--//generate random number from 0 to 1000 var i = Math.floor (Ma Th.random () *1000); document.write (i); document.write ("<br>")//Generate random number between 1~10 var j = Math.floor (Math.random () *10) +1; document.write (j); --> </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


JavaScript Random number advanced application
① since JavaScript, many browsers have built-in random number generation methods. For example:
var number = Math.random ();
This method produces a floating-point number between 0 and 1.
② is based on time and can also produce random numbers. For example:
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.
③ here introduces a fairly excellent random number generator program that can be applied in many fields.
Copy Code code as follows:

<script language= "JavaScript" ><!--
The 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 of the randomizer. -->
</script>

Two, VBScript (ASP) random number
VBS generates random numbers
A random number without a specified range
Copy Code code as follows:

Function getrandomize (NUMSTR)
Randomize
Getrandomize = Int ((NUMSTR * Rnd) + 1)
End Function

Generate random numbers for a specified range
Copy Code code as follows:

Function Myrnd (Nummin,nummax)
Randomize
Myrnd = Int (Rnd * (nummax-nummin + 1)) + Nummin
End Function

Here are some of the JS random number of instance code
Copy Code code as follows:

<script>
Declare a random number variable, default to 1
var getrandomn = 1;
To get a function of a value in a random range
function Getrandom (n) {Getrandomn=math.floor (Math.random () *n+1)}
Start call, get a random number of 1-100
Getrandom ("100");
Output view
document.write (GETRANDOMN)
</script>

Is it pretty neat code? Getrandomn is a variable of random numbers that can be invoked arbitrarily.
Give me a random display of the special effects code:
<script>
Now, let's start with a random number from 1 to 3.
Getrandom ("3");
Call items that match random numbers
Switch (GETRANDOMN) {
Case 1:
document.write ("When random numbers are 1 show cloud-dwelling communities")
Break
Case 2:
document.write ("Show www.jb51.net when random number is 2")
Break
}
</script>

Demo Code:
<script> var getrandomn = 1; function Getrandom (n) {Getrandomn=math.floor (Math.random () *n+1)} getrandom ("2"); Switch (getrandomn) {case 1:document.write ("When random number is 1 showing cloud-dwelling community") break; Case 2:document.write ("When random number is 2 show www.jb51.net") break; } </script>
[ctrl+a All selected note: If you need to introduce external JS need to refresh to perform]
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.