どうも、ちょげ(@chogetarou)です。
乱数を1から10の範囲で生成する方法を紹介します。
方法

乱数を1から10の範囲で生成するには、Math.random()を使います。
まず、Math.floor()を呼び出します。
Math.floor()の引数で、Math.random()の戻り値に10を掛けます。
そして、Math.floor()の戻り値に「1」を足します。
var random = Math.floor(Math.random() * 10) + 1
上記のMath.floor()に1を足した値は、1から10の範囲の乱数になります。
使用例
for (var i = 0; i < 5; i++) {
console.log(Math.floor(Math.random() * 10) + 1);
}
出力:
5
10
3
9
3
コメント