Javascript实现随机整数

 一个简单的函数,产生随机整数:

 

  1. function rnd(start, end){
  2.     return Math.floor(Math.random() * (end - start) + start);
  3. }

 

如rnd(10, 100)将产生10~100之间的整数,其中Math.random()产生0-1的随机数,乘以(100-10)将得到0~90的数,再加上10得到10~100的数,然后用Math.floor对该结果取整。

posted @ 2009-01-02 10:28  moonsnow  阅读(237)  评论(0编辑  收藏  举报