JavaScript random()メソッド

Math 对象参考手册JavaScript Math オブジェクト

例

0(含む)〜 1(含まない)の間の乱数を返します:

Math.random();

出力結果:


試してみる »

定義と用法

random() メソッドは、0(含む)〜 1(含まない)の間の乱数を返します。


ブラウザサポート

Internet ExplorerFirefoxOperaGoogle ChromeSafari

すべての主要ブラウザが random() メソッドをサポートしています。


構文

Math.random()

戻り値

型 説明
Number 0.0 〜 1.0(含まない)の間の擬似乱数。

技術詳細

JavaScript バージョン: 1.0


その他の例

例

この例では、1 から 10 までの間の乱数を取得します:

Math.floor((Math.random()*10)+1);

出力結果:


試してみる »

例

この例では、1 から 100 までの間の乱数を取得します:

Math.floor((Math.random()*100)+1);

出力結果:


試してみる »

例

以下の関数は、min(含む)〜 max(含まない)の間の数値を返します:

function getRndInteger(min, max) { return Math.floor(Math.random() * (max - min) ) + min; }

試してみる »

例

以下の関数は、min(含む)〜 max(含む)の間の数値を返します:

function getRndInteger(min, max) { return Math.floor(Math.random() * (max - min + 1) ) + min; }

試してみる »


Math 对象参考手册JavaScript Math オブジェクト

その他の拡張