aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/javascript/reference/global_objects/math/random/index.html
blob: 3fa9b6d89f9583b2b8dce64eda43a75352f39f3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
---
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>

<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>