aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/javascript/reference/global_objects/math/random
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/ja/web/javascript/reference/global_objects/math/random
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/ja/web/javascript/reference/global_objects/math/random')
-rw-r--r--files/ja/web/javascript/reference/global_objects/math/random/index.html101
1 files changed, 101 insertions, 0 deletions
diff --git a/files/ja/web/javascript/reference/global_objects/math/random/index.html b/files/ja/web/javascript/reference/global_objects/math/random/index.html
new file mode 100644
index 0000000000..18d04e917a
--- /dev/null
+++ b/files/ja/web/javascript/reference/global_objects/math/random/index.html
@@ -0,0 +1,101 @@
+---
+title: Math.random()
+slug: Web/JavaScript/Reference/Global_Objects/Math/random
+tags:
+ - JavaScript
+ - Math
+ - Math.random()
+ - Method
+ - Random
+ - Reference
+translation_of: Web/JavaScript/Reference/Global_Objects/Math/random
+---
+<div>{{JSRef}}</div>
+
+<p><strong><code>Math.random()</code></strong>関数は、 0 以上 1 未満 (0 は含むが、 1 は含まない) の範囲で浮動小数点の擬似乱数を返します。その範囲ではほぼ均一な分布で、ユーザーは範囲の拡大をすることができます。実装側で乱数生成アルゴリズムの初期シードを選択します。ユーザーが初期シードを選択、またはリセットすることは出来ません。</p>
+
+<div>{{EmbedInteractiveExample("pages/js/math-random.html")}}</div>
+
+<div class="note">
+<p><code>Math.random()</code> の提供する乱数は、暗号に使用可能な安全性を備えて<em>いません</em>。セキュリティに関連する目的では使用しないでください。代わりに Web Crypto API (より具体的には {{domxref("Crypto.getRandomValues", "window.crypto.getRandomValues()")}} メソッド) を使用してください。</p>
+</div>
+
+<h2 id="Syntax" name="Syntax">構文</h2>
+
+<pre class="syntaxbox notranslate">Math.random()</pre>
+
+<h3 id="Return_value" name="Return_value">返値</h3>
+
+<p><code>0</code> (含む) から 1 (含まない) までの擬似乱数である浮動小数点数です。</p>
+
+<h2 id="Examples" name="Examples">例</h2>
+
+<p>JavaScript における数値は、IEEE 754 浮動小数点での round-to-nearest-even を行うため、以下の関数の値域が ( <code>Math.random()</code> 自体の値域が正しくても) 厳密ではないことに注意してください。非常に大きい境界値 (2<sup>53</sup> 以上) のうち<em>極めて</em>稀な数値で、通常なら返されないはずの上限値が出力されてしまうことがあり得ます。</p>
+
+<h3 id="Getting_a_random_number_between_0_inclusive_and_1_exclusive" name="Getting_a_random_number_between_0_inclusive_and_1_exclusive">0 以上 1 未満の乱数を得る</h3>
+
+<pre class="brush: js notranslate">function getRandom() {
+ return Math.random();
+}
+</pre>
+
+<h3 id="Getting_a_random_number_between_two_values" name="Getting_a_random_number_between_two_values">2 つの値の間の乱数を得る</h3>
+
+<p>この例は指定した値の間の乱数を返します。返値は <code>min</code> 以上、 <code>max</code> 未満です。</p>
+
+<pre class="brush: js notranslate">function getRandomArbitrary(min, max) {
+ return Math.random() * (max - min) + min;
+}
+</pre>
+
+<h3 id="Getting_a_random_integer_between_two_values" name="Getting_a_random_integer_between_two_values">2 つの値の間のランダムな整数を得る</h3>
+
+<p>この例は指定した値の間のランダムな整数を返します。返値は <code>min</code> 以上 (<code>min</code> が整数でない場合、 <code>min</code> より大きい次の整数以上)、 <code>max</code> 未満です。</p>
+
+<pre class="brush: js notranslate">function getRandomInt(min, max) {
+  min = Math.ceil(min);
+  max = Math.floor(max);
+ return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
+}
+</pre>
+
+<div class="note">
+<p><code>Math.round()</code> を使う方が魅力的かもしれませんが、その場合は乱数が不均一な分布に従うことになるので、ユーザーのニーズに合わないかもしれません。</p>
+</div>
+
+<h3 id="Getting_a_random_integer_between_two_values_inclusive" name="Getting_a_random_integer_between_two_values_inclusive">包括的に 2 つの値の間のランダムな整数を得る</h3>
+
+<p>上記の <code>getRandomInt()</code> 関数が返す乱数の範囲は <code>min</code> を含みますが、<code>max</code> は除外されます。 <code>min</code> も <code>max</code> も範囲に含まれた乱数を生成したいなら、以下の <code>getRandomIntInclusive()</code> 関数を使うといいでしょう。</p>
+
+<pre class="brush: js notranslate">function getRandomIntInclusive(min, max) {
+  min = Math.ceil(min);
+  max = Math.floor(max);
+ return Math.floor(Math.random() * (max - min + 1) + min); //The maximum is inclusive and the minimum is inclusive
+}</pre>
+
+<h2 id="Specifications" name="Specifications">仕様書</h2>
+
+<table class="standard-table">
+ <thead>
+ <tr>
+ <th scope="col">仕様書</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-math.random', 'Math.random')}}</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2>
+
+<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div>
+
+<p>{{Compat("javascript.builtins.Math.random")}}</p>
+
+<h2 id="See_also" name="See_also">関連情報</h2>
+
+<ul>
+ <li>{{domxref("Crypto.getRandomValues", "window.crypto.getRandomValues()")}}</li>
+</ul>