Javascript生成隨機數

Javascript生成隨機數

1.內置隨機數生成器

Math.random(); //該方法產生一個0到1之間的浮點數。Math.floor(Math.random()*10+1); //1-10Math.floor(Math.random()*24);//0-23

2.基於時間的隨機生成器

var now=new Date(); 
var number = now.getSeconds(); //這將產生一個基於目前時間的0到59的整數。 var now=new Date(); 
var number = now.getSeconds()%43; //這將產生一個基於目前時間的0到42的整數。

3.一個優秀的Javascript隨機數生成器

<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>
相關文章
相關標籤/搜索