javascript随机数生成公式

所以,如果你希望生成1到任意值的随机数,公式就是这样的:

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

如果你希望生成0到任意值的随机数,公式就是这样的:

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

如果你希望生成任意值到任意值的随机数,公式就是这样的:

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

原文地址:https://www.cnblogs.com/xienh/p/5213026.html