จาวาสคริปต์สุ่ม


สารบัญ

    แสดงสารบัญ


Math.random()

Math.random() ส่งคืนตัวเลขสุ่มระหว่าง 0 (รวม) และ 1 (พิเศษ):

ตัวอย่าง

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

ลองด้วยตัวคุณเอง →

<!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() ส่งคืนตัวเลขที่ต่ำกว่า 1 เสมอ


JavaScript จำนวนเต็มสุ่ม

Math.random() ใช้กับ Math.floor() สามารถใช้เพื่อส่งคืนจำนวนเต็มแบบสุ่ม

ไม่มีสิ่งที่เรียกว่าจำนวนเต็ม JavaScript

เรากำลังพูดถึงตัวเลขที่ไม่มีทศนิยมที่นี่

ตัวอย่าง

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

ลองด้วยตัวคุณเอง →

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

ตัวอย่าง

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

ลองด้วยตัวคุณเอง →

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

ตัวอย่าง

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

ลองด้วยตัวคุณเอง →

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

ตัวอย่าง

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

ลองด้วยตัวคุณเอง →

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

ตัวอย่าง

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

ลองด้วยตัวคุณเอง →

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

ตัวอย่าง

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

ลองด้วยตัวคุณเอง →

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


ฟังก์ชั่นสุ่มที่เหมาะสม

ดังที่คุณเห็นจากตัวอย่างด้านบน การสร้างฟังก์ชันสุ่มที่เหมาะสมอาจเป็นความคิดที่ดี เพื่อใช้สำหรับวัตถุประสงค์จำนวนเต็มสุ่มทั้งหมด

ฟังก์ชัน JavaScript นี้จะส่งกลับตัวเลขสุ่มระหว่าง min (รวมอยู่ด้วย) และเสมอ สูงสุด (ไม่รวม):

ตัวอย่าง

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

ลองด้วยตัวคุณเอง →

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

ฟังก์ชัน JavaScript นี้จะส่งกลับตัวเลขสุ่มระหว่างต่ำสุดและสูงสุดเสมอ (รวมทั้งสองอย่าง):

ตัวอย่าง

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

ลองด้วยตัวคุณเอง →

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