JavaScript Math.random()
例1
let x = Math.random();
定義と使用法
Math.random()メソッドは、0(包括的)から1(排他的)までの乱数を返します。
例2
1から10までの乱数を返します。
Math.floor((Math.random() * 10) + 1);
例3
1から100までの乱数を返します。
Math.floor((Math.random() * 100) + 1);
ブラウザのサポート
Math.random()
ECMAScript1(ES1)機能です。
ES1(JavaScript 1997)は、すべてのブラウザーで完全にサポートされています。
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |
構文
Math.random()
パラメーター
なし |
戻り値
0から1までの数値を表す数値。 |