JavaScript aléatoire


Table des matières

    Afficher la table des matières


Math.random()

Math.random() renvoie un nombre aléatoire compris entre 0 (inclus) et 1. (exclusif):

Exemple

// Returns a random number:
Math.random();

Essayez-le vous-même →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Math.random()</h2>

<p>Math.random() returns a random number between 0 (included) and 1 (excluded):</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = Math.random();
</script>

</body>
</html>

Math.random() renvoie toujours un nombre inférieur à 1.


Entiers aléatoires JavaScript

Math.random() utilisé avec Math.floor() peut être utilisé pour renvoyer des entiers aléatoires.

Les entiers JavaScript n’existent pas.

Nous parlons ici de nombres sans décimales.

Exemple

// Returns a random integer from 0 to 9:
Math.floor(Math.random() * 10);

Essayez-le vous-même →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Math</h2>

<p>Math.floor(Math.random() * 10) returns a random integer between 0 and 9 (both 
included):</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML =
Math.floor(Math.random() * 10);
</script>

</body>
</html>

Exemple

// Returns a random integer from 0 to 10:
Math.floor(Math.random() * 11);

Essayez-le vous-même →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Math</h2>

<p>Math.floor(Math.random() * 11) returns a random integer between 0 and 10 (both included):</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML =
Math.floor(Math.random() * 11);
</script>

</body>
</html>

Exemple

// Returns a random integer from 0 to 99:
Math.floor(Math.random() * 100);

Essayez-le vous-même →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Math</h2>

<p>Math.floor(Math.random() * 100)) returns a random integer between 0 and 99 
(both included):</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML =
Math.floor(Math.random() * 100);
</script>

</body>
</html>

Exemple

// Returns a random integer from 0 to 100:
Math.floor(Math.random() * 101);

Essayez-le vous-même →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Math</h2>

<p>Math.floor() used with Math.random() * 101 returns a random integer between 0 and 100 
(both included):</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML =
Math.floor(Math.random() * 101);
</script>

</body>
</html>

Exemple

// Returns a random integer from 1 to 10:
Math.floor(Math.random() * 10) + 1;

Essayez-le vous-même →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Math</h2>

<p>Math.floor(Math.random() * 10) + 1) returns a random integer between 1 and 10 
(both included):</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML =
Math.floor(Math.random() * 10) + 1;
</script>

</body>
</html>

Exemple

// Returns a random integer from 1 to 100:
Math.floor(Math.random() * 100) + 1;

Essayez-le vous-même →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Math</h2>

<p>Math.floor(Math.random() * 100) + 1) returns a random integer between 1 and 
100 (both included):</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML =
Math.floor(Math.random() * 100) + 1;
</script>

</body>
</html>


Une fonction aléatoire appropriée

Comme vous pouvez le voir dans les exemples ci-dessus, cela pourrait être une bonne idée de créer une fonction aléatoire appropriée. à utiliser à toutes fins de nombres entiers aléatoires.

Cette fonction JavaScript renvoie toujours un nombre aléatoire compris entre min (inclus) et maximum (exclu) :

Exemple

function getRndInteger(min, max) {
  return Math.floor(Math.random() * (max - min) ) + min;
}

Essayez-le vous-même →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Math.random()</h2>

<p>Every time you click the button, getRndInteger(min, max) returns a random number between 0 
and 9 (both included):</p>

<button onclick="document.getElementById('demo').innerHTML = getRndInteger(0,10)">Click Me</button>

<p id="demo"></p>

<script>
function getRndInteger(min, max) {
  return Math.floor(Math.random() * (max - min)) + min;
}
</script>

</body>
</html>

Cette fonction JavaScript renvoie toujours un nombre aléatoire compris entre min et max (tous deux inclus) :

Exemple

function getRndInteger(min, max) {
    return Math.floor(Math.random() * (max - min + 1) ) + min;
}

Essayez-le vous-même →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Math.random()</h2>

<p>Every time you click the button, getRndInteger(min, max) returns a random number between 1 and 10 (both included):</p>

<button onclick="document.getElementById('demo').innerHTML = getRndInteger(1,10)">Click Me</button>

<p id="demo"></p>

<script>
function getRndInteger(min, max) {
  return Math.floor(Math.random() * (max - min + 1) ) + min;
}
</script>

</body>
</html>