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
|
---
title: Math.random()
slug: Web/JavaScript/Reference/Global_Objects/Math/random
tags:
- 浮點數
- 隨機
translation_of: Web/JavaScript/Reference/Global_Objects/Math/random
---
<div>{{JSRef}}</div>
<p>函數 <strong><code>Math.random()</code></strong> 會回傳一個偽隨機小數 (pseudo-random) 介於0到1之間(包含 0,不包含1) ,大致符合數學與統計上的均勻分佈 (uniform distribution) ,您可以選定想要的數字區間,它會透過演算法被產生並且不允許使用者自行跳選或重設成特定數字。{{EmbedInteractiveExample("pages/js/math-random.html")}}</p>
<div class="note">
<p><code>Math.random()</code> 所產生的偽隨機小數不符合加密學安全性要求。<em>請勿使用於任何加密、資安相關領域。</em></p>
<p><em>如有加密需求建議參考Web Crypto API</em><a href="/en-US/docs/Web/API/RandomSource/getRandomValues" title="The documentation about this has not yet been written; please consider contributing!"><code>window.crypto.getRandomValues()</code></a></p>
</div>
<h2 id="語法">語法</h2>
<pre class="syntaxbox notranslate">Math.random()</pre>
<h3 id="回傳值_Return_value">回傳值 Return value</h3>
<p>回傳一個偽隨機小數 (pseudo-random),小數也稱浮點數; 介於0到1之間(包含 0,不包含1) 。</p>
<h2 id="範例">範例</h2>
<p>請留意JavaScript中的數字與許多語言一樣使用 IEEE 754 floating point numbers with round-to-nearest-even behavior, the ranges claimed for the functions below (excluding the one for <code>Math.random()</code> itself) aren't exact. If extremely large bounds are chosen (2<sup>53</sup> or higher), it's possible in <em>extremely</em> rare cases to calculate the usually-excluded upper bound.</p>
<h3 id="Getting_a_random_number_between_0_inclusive_and_1_exclusive">Getting a random number between 0 (inclusive) and 1 (exclusive)</h3>
<pre class="brush: js notranslate">function getRandom() {
return Math.random();
}
</pre>
<h3 id="Getting_a_random_number_between_two_values">Getting a random number between two values</h3>
<p>This example returns a random number between the specified values. The returned value is no lower than (and may possibly equal) <code>min</code>, and is less than (and not equal) <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">Getting a random integer between two values</h3>
<p>This example returns a random <em>integer</em> between the specified values. The value is no lower than <code>min</code> (or the next integer greater than <code>min</code> if <code>min</code> isn't an integer), and is less than (but not equal to) <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>It might be tempting to use <code>Math.round()</code> to accomplish that, but doing so would cause your random numbers to follow a non-uniform distribution, which may not be acceptable for your needs.</p>
</div>
<h3 id="Getting_a_random_integer_between_two_values_inclusive">Getting a random integer between two values, inclusive</h3>
<p>While the <code>getRandomInt()</code> function above is inclusive at the minimum, it's exclusive at the maximum. What if you need the results to be inclusive at both the minimum and the maximum? The <code>getRandomIntInclusive()</code> function below accomplishes that.</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">Specifications</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-math.random', 'Math.random')}}</td>
</tr>
</tbody>
</table>
<h2 id="瀏覽器相容性">瀏覽器相容性</h2>
<p>{{Compat("javascript.builtins.Math.random")}}</p>
<h2 class="countTop" id="其他參考資料">其他參考資料</h2>
<ul>
<li><a href="/en-US/docs/Web/API/RandomSource/getRandomValues" title="The documentation about this has not yet been written; please consider contributing!"><code>window.crypto.getRandomValues()</code></a></li>
</ul>
|