JS Random Number Foundation
First, is the 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 number returned is called "pseudo random" because it is technically calculated in a confidential manner.
Return
number─ a number.
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
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.
<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>
Detailed Source reference: http://www.jb51.net/article/12566.htm
So how can you easily get a random number in a given range? After my comparison, this function of mine is so far the most concise practical drop on the Internet
Instance 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 SELECT Note: If the need to introduce external JS need to refresh to perform]
Code Description:
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>
Current 1/2 page
12 Next read the full text