Math.random()
Math.random()
ﯽﻣﺮﺑ 1 ﻭ ،(ﻞﻣﺎﺷ) 0 ﻦﯿﺑ ﯽﻓﺩﺎﺼﺗ ﺩﺪﻋ ﮏﯾu
// 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 ﺯﺍ ﺮﺘﻤﮐ ﯼﺩﺪﻋ ﻪﺸﯿﻤﻫﺪﻧﺍﺩﺮ
.ﺩﺭﺍﺪﻧ ﺩﻮﺟﻭ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺢﯿﺤﺻ ﺩﺍﺪﻋﺍ ﻡﺎﻧ ﻪﺑ ﯼﺰﯿﭼ .ﻢﯿﻨﮐ ﯽﻣ ﺖﺒﺤﺻ ﺭﺎﺸﻋﺍ ﻥﻭﺪﺑ ﺩﺍﺪﻋﺍ ﺩﺭﻮﻣ ﺭﺩ ﺎﺠﻨﯾﺍ ﺭﺩ ﺎﻣMath.random()
ﺎﺑ ﻩﺪﺷ ﻩﺩﺎﻔﺘﺳﺍ
// 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>
ﺪﺷﺎﺑ ﺐﺳﺎﻨﻣ ﯽﻓﺩﺎﺼﺗ ﻊﺑﺎﺗ ﮏﯾ ﺩﺎﺠﯾﺍ ﯼﺍﺮﺑ ﯽﺑﻮﺧ ﻩﺪﯾﺍ ﺖﺳﺍ ﻦﮑﻤﻣ ،ﺪﯿﻨﯿﺑ ﯽﻣ ﻻﺎﺑ ﯼﺎﻫ ﻝﺎﺜﻣ ﺯﺍ ﻪﮐ
ﻭ (ﻞﻣﺎﺷ) 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>
:(ﻞﻣﺎﺷ ﻭﺩ ﺮﻫ) ﺪﻧﺍﺩﺮﮔ ﯽﻣﺮﺑ max ﻭ min ﻦﯿﺑ ﯽﻓﺩﺎﺼﺗ ﺩﺪﻋ ﮏﯾ ﻪﺸﯿﻤﻫ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻊﺑﺎﺗ ﻦﯾﺍ
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>