【转】JS生成指定范围内的随机数(支持随机小数)

https://www.cnblogs.com/mq0036/p/9139231.html

所以生成[1,max]的随机数,公式如下:

// max - 期望的最大值
parseInt(Math.random()*max,10)+1;
Math.floor(Math.random()*max)+1;
Math.ceil(Math.random()*max);

所以生成[0,max]到任意数的随机数,公式如下:

// max - 期望的最大值
parseInt(Math.random()*(max+1),10);
Math.floor(Math.random()*(max+1));

所以希望生成[min,max]的随机数,公式如下:

// max - 期望的最大值
// min - 期望的最小值
parseInt(Math.random()*(max-min+1)+min,10);
Math.floor(Math.random()*(max-min+1)+min);

 

posted @ 2019-11-14 11:00  数学系的挨踢者  阅读(1262)  评论(0编辑  收藏  举报