Javascript Random Integer Between two Numbers
我试图创建一个随机数生成器,在两个数字之间生成随机数。例如,假设我想生成一个4到10之间的随机数,我希望它能够从4到10之间的任意数字中进行选择。以下是我的尝试:
1 2 3 | var randNumMin = 4; var randNumMax = 10; var randInt = (Math.floor(Math.random() * (randNumMax - randNumMin + 1)) + randNumMin); |
然而,这似乎不起作用,产生了不在4到10之间的奇怪的随机数,有些以0开头。正确的算法是什么?
下面是我在以下代码中实现算法的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | $(function() { $('#generateRandNums').click(function() { var numCount = document.getElementById("randNumCount").value; var randNumMin = document.getElementById("randNumMin").value; var randNumMax = document.getElementById("randNumMax").value; if (numCount.match(/^[\d]*$/ ) && randNumMin.match(/^[\d]*$/ ) && randNumMax.match(/^[\d]*$/ )) { if (numCount =="" || randNumMin =="" || randNumMax =="") { alert ("Please fill out all forms then try again."); } else { if (randNumMin>randNumMax) { alert ("Please make sure your first number is smaller than the second, then try again."); } else { if (randNumMin<0) { alert ("Please make sure that you generate a positive number of random numbers, then try again."); } else { if (numCount>1) { var randResult = ("You generated" + numCount +" random numbers between" + randNumMin +" and" + randNumMax +" and got the numbers") oneNumber = 0; } else { var randResult = ("You generated a random number between" + randNumMin +" and" + randNumMax +" and got the number"); oneNumber = 1; } for (i=0;i<numCount;i++) { //Get a random number between randNumMin and randNumMax var randInt = (Math.floor(Math.random() * (randNumMax - randNumMin + 1)) + randNumMin); if (i == numCount-1) { if (oneNumber == 0) { randResult = (randResult +"and" + randInt +"."); } else { randResult = (randResult + randInt +"."); } } else { randResult = (randResult + randInt +","); } } $("#randNumResults").val(randResult); } } } } else { alert ("Make sure you only enter numbers and no spaces, then try again."); } }); }); |
我还试着用这个替换兰丁线:
1 | var randInt = Math.floor((Math.random() * ((randNumMax + 1) - randNumMin)) + randNumMin); |
它仍然不起作用。我不确定算法是错误的,还是把它错误地合并到函数中。感谢您的回答。
在特定范围内用javascript生成随机整数?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /** * Returns a random number between min and max */ function getRandomArbitary (min, max) { return Math.random() * (max - min) + min; } /** * Returns a random integer between min and max * Using Math.round() will give you a non-uniform distribution! */ function getRandomInt (min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } |
http://roshanbh.com.np/2008/09/get-random-number-range-two-numbers-javascript.html
1 2 3 4 5 6 | //function to get random number upto m function randomXToY(minVal,maxVal,floatVal) { var randVal = minVal+(Math.random()*(maxVal-minVal)); return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal); } |
或
在javascript中生成两个数字之间的随机数
对于这个随机变量(最小值,最大值),js有一个很好的实用程序。
您的问题是您从未将字符串转换为数字,请尝试添加此字符串
1 2 3 4 5 6 7 | if ( numCount.match(/^[\d]*$/ ) && randNumMin.match(/^[\d]*$/ ) && randNumMax.match(/^[\d]*$/ )){ if (numCount ==="" || randNumMin ==="" || randNumMax ==="") { alert ("Please fill out all forms then try again."); } else { numCount=numCount-0;randNumMin=randNumMin-0;randNumMax=randNumMax-0; |
另一个注意事项是,您需要将检查值是否为空字符串更改为严格相等。为了了解我的意思,尝试对其中一个值使用零。
这个怎么样?
1 2 3 | var max = 10; var min = 4; var random = Math.floor((Math.random() * ((max + 1) - min)) + min); |