aboutsummaryrefslogtreecommitdiff
path: root/files/ca/web/javascript/reference/global_objects/math/random
diff options
context:
space:
mode:
Diffstat (limited to 'files/ca/web/javascript/reference/global_objects/math/random')
-rw-r--r--files/ca/web/javascript/reference/global_objects/math/random/index.html126
1 files changed, 126 insertions, 0 deletions
diff --git a/files/ca/web/javascript/reference/global_objects/math/random/index.html b/files/ca/web/javascript/reference/global_objects/math/random/index.html
new file mode 100644
index 0000000000..d70169efd4
--- /dev/null
+++ b/files/ca/web/javascript/reference/global_objects/math/random/index.html
@@ -0,0 +1,126 @@
+---
+title: Math.random()
+slug: Web/JavaScript/Referencia/Objectes_globals/Math/random
+translation_of: Web/JavaScript/Reference/Global_Objects/Math/random
+---
+<div>{{JSRef}}</div>
+
+<p>La funció <strong><code>Math.random()</code></strong> retorna un nombre decimal de punt flotant pseudo-aleatori que roman dins el rang <code>[0, 1)</code>, és a dir, des de 0 (inclòs) fins a 1 (exclòs), que després pot ser escalat al rang dessitjat. La implementació selecciona la llavor inicial per a l'algoritme generador de nombres aleatoris; aquesta llavor no pot ser resetejada o escollida per l'usuari.</p>
+
+<div class="note">
+<p><strong>Nota:</strong> <code>Math.random()</code> <strong>no</strong> proporciona nombres aleatoris criptogràficament segurs. No l'utilitzeu per a cap tasca relacionada amb la seguretat. Per a aquest ús utilitzeu la API Web Crypto, i més concretament el mètode {{domxref("RandomSource.getRandomValues()", "window.crypto.getRandomValues()")}}.</p>
+</div>
+
+<h2 id="Sintaxi">Sintaxi</h2>
+
+<pre class="syntaxbox"><code>Math.random()</code></pre>
+
+<h2 id="Exemples">Exemples</h2>
+
+<h3 id="Utilitzar_Math.random()">Utilitzar <code>Math.random()</code></h3>
+
+<p>Cal destacar que com que els nombres a JavaScript són nombres de punt flotant IEEE 754 amb comportament d'arrodoniment al parell més proper, els rangs proclamats per les funcions de sota (a excepció de <code>Math.random()</code>) no són exactes. Si s'escullen límits extremadament grans (2<sup>53</sup> o majors), és possible en casos <em>extremadament</em> rars, obtindre el límit superior que normalment és exclòs.</p>
+
+<pre class="brush: js">// Retorna un nombre aleatori entre 0 (inclòs) i 1 (exclòs)
+function getRandom() {
+ return Math.random();
+}
+</pre>
+
+<pre class="brush: js">// Retorna un nombre aleatori entre min (inclòs) i max (exclòs)
+function getRandomArbitrary(min, max) {
+ return Math.random() * (max - min) + min;
+}
+</pre>
+
+<pre class="brush: js">// Retorna un nombre sencer aleatori entre min (inclòs) i max (exclòs)
+// Utilitzar Math.round() proporciona una distribució no uniforme!
+function getRandomInt(min, max) {
+ return Math.floor(Math.random() * (max - min)) + min;
+}
+</pre>
+
+<pre class="brush: js">// Returns a random integer between min (included) and max (included)
+// Using Math.round() will give you a non-uniform distribution!
+function getRandomIntInclusive(min, max) {
+ return Math.floor(Math.random() * (max - min + 1)) + min;
+}</pre>
+
+<h2 id="Especificacions">Especificacions</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">Especificació</th>
+ <th scope="col">Estat</th>
+ <th scope="col">Comentaris</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES1')}}</td>
+ <td>{{Spec2('ES1')}}</td>
+ <td>Definició inicial. JavaScript 1.0 (Només UNIX) / JavaScript 1.1 (Totes les plataformes).</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES5.1', '#sec-15.8.2.14', 'Math.random')}}</td>
+ <td>{{Spec2('ES5.1')}}</td>
+ <td> </td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES6', '#sec-math.random', 'Math.random')}}</td>
+ <td>{{Spec2('ES6')}}</td>
+ <td> </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2>
+
+<div>{{CompatibilityTable}}</div>
+
+<div id="compat-desktop">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Característica</th>
+ <th>Chrome</th>
+ <th>Firefox (Gecko)</th>
+ <th>Internet Explorer</th>
+ <th>Opera</th>
+ <th>Safari</th>
+ </tr>
+ <tr>
+ <td>Suport bàsic</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<div id="compat-mobile">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Característica</th>
+ <th>Android</th>
+ <th>Chrome for Android</th>
+ <th>Firefox Mobile (Gecko)</th>
+ <th>IE Mobile</th>
+ <th>Opera Mobile</th>
+ <th>Safari Mobile</th>
+ </tr>
+ <tr>
+ <td>Suport bàsic</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ </tr>
+ </tbody>
+</table>
+</div>