1. Use the built-in random number generation method:
The code copy is as follows:
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, random numbers can also be generated:
The code copy is as follows:
var now=new Date();
var number = now.getSeconds(); // This will produce an integer from 0 to 59 based on the current time.
var now=new Date();
var number = now.getSeconds()%43; // This will produce an integer from 0 to 42 based on the current time.
3. A very excellent random number generator program can be applied in many fields.
Program code
The code copy is as follows:
<script language="JavaScript"><!--
// The Central Randomizer 1.3 (C) 1997 by Paul Houle ([email protected])
// 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>