首页 > 网站 > WEB开发 > 正文

javascript获取随机整数

2024-04-27 14:01:51
字体:
来源:转载
供稿:网友

最近发现两个重写Math.round方法的实现:

Math.rand = function(l,u)
{
     return Math.floor((Math.random() * (u-l+1))+l);
}

Math.PRototype.rand = function(l,u)
{
     return Math.floor((Math.random() * (u-l+1))+l);
}

Sample: Math.rand(1,10)

大家说这两个方法都可以吗? 那个是正确的做法?呵呵,测试一下就知道了:)


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表