【转】Javascript产生随机数

<script>function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script>

创建随机数

①自JavaScript产生后,好多浏览器中都有内置的随机数发生方法。例如:

var number = Math.random();

该方法产生一个0到1之间的浮点数。

②基于时间,亦可以产生随机数。例如:

var now=new Date();

var number = now.getSeconds();

这将产生一个基于目前时间的0到59的整数。

var now=new Date();

var number = now.getSeconds()%43;

这将产生一个基于目前时间的0到42的整数。

③这里介绍一个相当优秀的的随机数发生器程序,能应用于许多领域。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值