javascript 隨機數區間

看風景就發表於2016-01-08

生成[0,max]之間的隨機數

parseInt(Math.random()*(max+1),10);
Math.floor(Math.random()*(max+1));

生成[1,max]之間的隨機數

parseInt(Math.random()*(max)+1,10);
Math.floor(Math.random()*(max)+1);

生成[min,max]之間的隨機數

parseInt(Math.random()*(max-min+1)+min,10);
Math.floor(Math.random()*(max-min+1)+min);

相關文章