乱数を出力するプログラム
0~10の間のランダムな値を返す
var rand = Math.floor(Math.random() * 11);
console.log( rand );
=> 3 // 実行するごとに1~10の範囲でランダムな値を返す
0~引数に渡した値より小さい乱数を返す関数
function randomInt(num){
var rand = Math.floor(Math.random() * num);
return rand;
}
randomInt(100);
=> 78 // 実行するごとに1~10の範囲でランダムな値を返す
最大値と最小値を渡し、ランダムな値を返す
function randomIntMinMax(min, max){
var rand = Math.floor(Math.random() * (max + 1 - min)) + min
return rand
}
randomIntMinMax(10, 100);
=> 78 // 実行するごとに10~100の範囲でランダムな値を返す