JavaScript slumpmässigt


Innehållsförteckning

    Visa innehållsförteckning


Math.random()

Math.random() returnerar ett slumptal mellan 0 (inklusive), och 1 (exklusiv):

Exempel

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

Prova själv →

<!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() returnerar alltid ett tal lägre än 1.


JavaScript slumpmässiga heltal

Math.random() som används med Math.floor() kan användas för att returnera slumpmässiga heltal.

Det finns inget som heter JavaScript-heltal.

Vi talar om siffror utan decimaler här.

Exempel

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

Prova själv →

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

Exempel

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

Prova själv →

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

Exempel

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

Prova själv →

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

Exempel

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

Prova själv →

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

Exempel

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

Prova själv →

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

Exempel

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

Prova själv →

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


En korrekt slumpmässig funktion

Som du kan se av exemplen ovan kan det vara en bra idé att skapa en ordentlig slumpmässig funktion att använda för alla slumpmässiga heltalsändamål.

Denna JavaScript-funktion returnerar alltid ett slumptal mellan min (ingår) och max (exkluderat):

Exempel

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

Prova själv →

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

Denna JavaScript-funktion returnerar alltid ett slumpmässigt tal mellan min och max (båda inkluderade):

Exempel

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

Prova själv →

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