# Javascript to generate random Numbers

• 2020-03-30 02:59:59
• OfStack

1. Use the built-in random number generation method:

``````
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. Random Numbers can also be generated based on time:

``````
var now=new Date();
var number = now.getSeconds(); //This produces an integer from 0 to 59 based on the current time.
var now=new Date();
var number = now.getSeconds()%43; //This produces an integer from 0 to 42 based on the current time.
``````

3. A fairly good random number generator program, can be applied to many fields.
The program code

``````
<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>
``````

Related articles: