diff options
| author | Masahiro FUJIMOTO <mfujimot@gmail.com> | 2021-09-04 00:46:12 +0900 |
|---|---|---|
| committer | Masahiro FUJIMOTO <mfujimot@gmail.com> | 2021-09-04 00:46:12 +0900 |
| commit | fe6f6abf2b7c497bf1f97f73a82dde7cf48eb79f (patch) | |
| tree | 51b7edfc370236684a203f4e69ae67bb7d24b549 /files/fr/web/javascript/reference/global_objects/math/random | |
| parent | 04ea4edc83cc12142ed151bbea2c65cffc8e76f6 (diff) | |
| parent | eeb07fe338cdc90092841d717919f46f9d9e3ff9 (diff) | |
| download | translated-content-fe6f6abf2b7c497bf1f97f73a82dde7cf48eb79f.tar.gz translated-content-fe6f6abf2b7c497bf1f97f73a82dde7cf48eb79f.tar.bz2 translated-content-fe6f6abf2b7c497bf1f97f73a82dde7cf48eb79f.zip | |
Merge branch 'main' into 20210818-Glossary/Type
Diffstat (limited to 'files/fr/web/javascript/reference/global_objects/math/random')
| -rw-r--r-- | files/fr/web/javascript/reference/global_objects/math/random/index.html | 113 | ||||
| -rw-r--r-- | files/fr/web/javascript/reference/global_objects/math/random/index.md | 92 |
2 files changed, 92 insertions, 113 deletions
diff --git a/files/fr/web/javascript/reference/global_objects/math/random/index.html b/files/fr/web/javascript/reference/global_objects/math/random/index.html deleted file mode 100644 index c5b3df93df..0000000000 --- a/files/fr/web/javascript/reference/global_objects/math/random/index.html +++ /dev/null @@ -1,113 +0,0 @@ ---- -title: Math.random() -slug: Web/JavaScript/Reference/Global_Objects/Math/random -tags: - - JavaScript - - Math - - Méthode - - Reference -translation_of: Web/JavaScript/Reference/Global_Objects/Math/random -original_slug: Web/JavaScript/Reference/Objets_globaux/Math/random ---- -<div>{{JSRef}}</div> - -<p>La fonction <code><strong>Math.random()</strong></code> renvoie un nombre flottant pseudo-aléatoire compris dans l'intervalle <code>[0, 1[</code> (ce qui signifie que 0 est compris dans l'intervalle mais que 1 en est exclu) selon une distribution approximativement uniforme sur cet intervalle. Ce nombre peut ensuite être multiplié afin de couvrir un autre intervalle. La graine (<em>seed</em>) du générateur est choisie par l'algorithme et ne peut pas être choisie ou réinitialisée par l'utilisateur.</p> - -<div>{{EmbedInteractiveExample("pages/js/math-random.html")}}</div> - -<div class="note"> -<p><strong>Note :</strong> <code>Math.random()</code> <strong>ne fournit pas</strong> de nombres aléatoires propres à une cryptographie sécurisée. Les résultats de cette méthode ne doivent pas être utilisées dans des applications liées à la sécurité. À la place, on préfèrera utiliser l'API Web Crypto et plus précisément la méthode {{domxref("RandomSource.getRandomValues()", "window.crypto.getRandomValues()")}}.</p> -</div> - -<h2 id="Syntaxe">Syntaxe</h2> - -<pre class="syntaxbox notranslate">Math.random()</pre> - -<h3 id="Valeur_de_retour">Valeur de retour</h3> - -<p>Un nombre flottant pseudo-aléatoire, généré entre 0 (inclus) et 1 (exclu)</p> - -<h2 id="Exemples">Exemples</h2> - -<p>En JavaScript, les nombres sont représentés comme des nombres flottants selon la norme IEEE 754 et les arrondis sont pris aux plus près. Aussi, les intervalles revendiqués par les fonctions ci-après (en dehors de <code>Math.random()</code>) ne sont pas théoriquement et précisément exacts. Si on utilise des bornes supérieures très grande (2^53 ou plus), il est alors possible, dans de très rares cas, d'obtenir la borne supérieure comme résultat alors que celle-ci devrait être exclue de l'intervalle.</p> - -<h3 id="Obtenir_un_nombre_aléatoire_entre_0_et_1">Obtenir un nombre aléatoire entre 0 et 1</h3> - -<pre class="brush: js notranslate">// On renvoie un nombre aléatoire entre 0 (inclus) et 1 (exclus) -function getRandom() { - return Math.random(); -}</pre> - -<h3 id="Obtenir_un_nombre_aléatoire_dans_un_intervalle">Obtenir un nombre aléatoire dans un intervalle</h3> - -<pre class="brush: js notranslate">// On renvoie un nombre aléatoire entre une valeur min (incluse) -// et une valeur max (exclue) -function getRandomArbitrary(min, max) { - return Math.random() * (max - min) + min; -}</pre> - -<h3 id="Obtenir_un_entier_aléatoire_dans_un_intervalle_ouvert_à_droite">Obtenir un entier aléatoire dans un intervalle ouvert à droite</h3> - -<pre class="brush: js notranslate">// On renvoie un entier aléatoire entre une valeur min (incluse) -// et une valeur max (exclue). -// Attention : si on utilisait Math.round(), on aurait une distribution -// non uniforme ! -function getRandomInt(min, max) { - min = Math.ceil(min); - max = Math.floor(max); - return Math.floor(Math.random() * (max - min)) + min; -} -</pre> - -<div class="warning"> -<p><strong>Attention :</strong> Utiliser <code>Math.round()</code> entraînerait une distribution non-uniforme et réduirait le caractère aléatoire de la méthode.</p> -</div> - -<h3 id="Obtenir_un_entier_aléatoire_dans_un_intervalle_fermé">Obtenir un entier aléatoire dans un intervalle fermé</h3> - -<pre class="brush: js notranslate">// On renvoie un entier aléatoire entre une valeur min (incluse) -// et une valeur max (incluse). -// Attention : si on utilisait Math.round(), on aurait une distribution -// non uniforme ! -function getRandomIntInclusive(min, max) { - min = Math.ceil(min); - max = Math.floor(max); - return Math.floor(Math.random() * (max - min +1)) + min; -} -</pre> - -<h2 id="Spécifications">Spécifications</h2> - -<table class="standard-table"> - <tbody> - <tr> - <th scope="col">Spécification</th> - <th scope="col">État</th> - <th scope="col">Commentaires</th> - </tr> - <tr> - <td>{{SpecName('ES1')}}</td> - <td>{{Spec2('ES1')}}</td> - <td>Définition initiale. Implémentée avec JavaScript 1.0 (UNIX) et 1.1 (toutes plateformes).</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> - <tr> - <td>{{SpecName('ESDraft', '#sec-math.random', 'Math.random')}}</td> - <td>{{Spec2('ESDraft')}}</td> - <td></td> - </tr> - </tbody> -</table> - -<h2 id="Compatibilité_des_navigateurs">Compatibilité des navigateurs</h2> - -<p>{{Compat("javascript.builtins.Math.random")}}</p> diff --git a/files/fr/web/javascript/reference/global_objects/math/random/index.md b/files/fr/web/javascript/reference/global_objects/math/random/index.md new file mode 100644 index 0000000000..e0ae51c992 --- /dev/null +++ b/files/fr/web/javascript/reference/global_objects/math/random/index.md @@ -0,0 +1,92 @@ +--- +title: Math.random() +slug: Web/JavaScript/Reference/Global_Objects/Math/random +tags: + - JavaScript + - Math + - Méthode + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/Math/random +original_slug: Web/JavaScript/Reference/Objets_globaux/Math/random +--- +{{JSRef}} + +La fonction **`Math.random()`** renvoie un nombre flottant pseudo-aléatoire compris dans l'intervalle `[0, 1[` (ce qui signifie que 0 est compris dans l'intervalle mais que 1 en est exclu) selon une distribution approximativement uniforme sur cet intervalle. Ce nombre peut ensuite être multiplié afin de couvrir un autre intervalle. La graine (_seed_) du générateur est choisie par l'algorithme et ne peut pas être choisie ou réinitialisée par l'utilisateur. + +{{EmbedInteractiveExample("pages/js/math-random.html")}} + +> **Note :** `Math.random()` **ne fournit pas** de nombres aléatoires propres à une cryptographie sécurisée. Les résultats de cette méthode ne doivent pas être utilisées dans des applications liées à la sécurité. À la place, on préfèrera utiliser l'API Web Crypto et plus précisément la méthode {{domxref("RandomSource.getRandomValues()", "window.crypto.getRandomValues()")}}. + +## Syntaxe + + Math.random() + +### Valeur de retour + +Un nombre flottant pseudo-aléatoire, généré entre 0 (inclus) et 1 (exclu) + +## Exemples + +En JavaScript, les nombres sont représentés comme des nombres flottants selon la norme IEEE 754 et les arrondis sont pris aux plus près. Aussi, les intervalles revendiqués par les fonctions ci-après (en dehors de `Math.random()`) ne sont pas théoriquement et précisément exacts. Si on utilise des bornes supérieures très grande (2^53 ou plus), il est alors possible, dans de très rares cas, d'obtenir la borne supérieure comme résultat alors que celle-ci devrait être exclue de l'intervalle. + +### Obtenir un nombre aléatoire entre 0 et 1 + +```js +// On renvoie un nombre aléatoire entre 0 (inclus) et 1 (exclus) +function getRandom() { + return Math.random(); +} +``` + +### Obtenir un nombre aléatoire dans un intervalle + +```js +// On renvoie un nombre aléatoire entre une valeur min (incluse) +// et une valeur max (exclue) +function getRandomArbitrary(min, max) { + return Math.random() * (max - min) + min; +} +``` + +### Obtenir un entier aléatoire dans un intervalle ouvert à droite + +```js +// On renvoie un entier aléatoire entre une valeur min (incluse) +// et une valeur max (exclue). +// Attention : si on utilisait Math.round(), on aurait une distribution +// non uniforme ! +function getRandomInt(min, max) { + min = Math.ceil(min); + max = Math.floor(max); + return Math.floor(Math.random() * (max - min)) + min; +} +``` + +> **Attention :** Utiliser `Math.round()` entraînerait une distribution non-uniforme et réduirait le caractère aléatoire de la méthode. + +### Obtenir un entier aléatoire dans un intervalle fermé + +```js +// On renvoie un entier aléatoire entre une valeur min (incluse) +// et une valeur max (incluse). +// Attention : si on utilisait Math.round(), on aurait une distribution +// non uniforme ! +function getRandomIntInclusive(min, max) { + min = Math.ceil(min); + max = Math.floor(max); + return Math.floor(Math.random() * (max - min +1)) + min; +} +``` + +## Spécifications + +| Spécification | État | Commentaires | +| ---------------------------------------------------------------------------- | ---------------------------- | ---------------------------------------------------------------------------------------- | +| {{SpecName('ES1')}} | {{Spec2('ES1')}} | Définition initiale. Implémentée avec JavaScript 1.0 (UNIX) et 1.1 (toutes plateformes). | +| {{SpecName('ES5.1', '#sec-15.8.2.14', 'Math.random')}} | {{Spec2('ES5.1')}} | | +| {{SpecName('ES6', '#sec-math.random', 'Math.random')}} | {{Spec2('ES6')}} | | +| {{SpecName('ESDraft', '#sec-math.random', 'Math.random')}} | {{Spec2('ESDraft')}} | | + +## Compatibilité des navigateurs + +{{Compat("javascript.builtins.Math.random")}} |
