javascript获取随机整数


最近发现两个重写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)

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

本文作者:
« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3