我正在尝试为计算损坏的桌面游戏制作一个简单的程序。所以我想在两个整数之间随机使用几个百分比来计算损伤。
问题是随机的并不适合我。我设置为最小值或最大值的数字并不重要,它总是从0开始到最大值之前的数字。
<script>
function showDiv() {
var armor = document.getElementById('armortype').value;
var damage = document.getElementById('dmgtype').value;
var min = document.getElementById('mindmg').value;
var max = document.getElementById('maxdmg').value;
document.getElementById('result').style.display = "block";
for (var i=0;i<100;i++)
{
var dmg_done = Math.floor(Math.random()*max+min+1);
document.getElementById('test').innerHTML += " " + dmg_done;
}
}
</script>
因此,对于min = 3,max = 6,我得到以下100个数字:
3 1 2 2 0 2 2 1 2 2 3 3 4 0 1 1 2 2 5 2 3 5 3 3 3 4 0 0 5 2 3 0 4 0 2 1 0 5 4 1 0 5 5 4 2 1 2 4 5 1 5 1 0 4 3 5 2 1 4 3 1 1 5 1 4 2 1 0 3 3 3 4 3 4 5 4 2 0 2 4 5 0 3 1 2 5 0 1 5 1 2 2 1 4 0 0 0 1 4 2
因此,min为3并不重要,它从0开始随机化,结果中甚至没有一个6。
答案 0 :(得分:0)
尝试将Math.floor(Math.random()*max+min+1)
替换为Math.floor(Math.random() * (max - min + 1) + min)
答案 1 :(得分:0)
那么为什么不尝试像这样计算随机
function getRandom (min, max) {
return Math.random() * (max - min + 1) + min;
}
编辑:添加到评论
var valOne = $("#input-1").val(),
valTwo = $("#input-2").val();
$("#button").click(function() {
$("#answer-input").val(getRandom(parseInt(valOne), parseInt(valTwo)));
);
答案 2 :(得分:0)
尝试
Math.floor((Math.random() * (max+1-min))+min);
max + 1 - 这样最大值包括,并且+ min使得min得到尊重
测试用例
var max = 6;
var min = 3;
var result = "";
for(var i=0;i<100;i++)
{
result += Math.floor((Math.random()*(max+1-min))+min) +",";
}
console.log(result);
结果(5次运行) 3,4,5,6,6,4,5,6,3,5,5,3,5,5,6,6,5,4,5,5,4,5,6,6,6, 5,6,3,4,3,5,3,6,6,6,3,6,3,5,6,5,4,6,6,6,5,5,4,3,5, 6,6,3,6,6,3,6,5,6,5,6,5,3,3,5,6,6,4,5,5,4,3,5,4,4, 4,3,5,4,5,5,3,3,4,4,6,3,3,3,4,4,3,6,3,4,4,3,3,4,6,
6,5,4,3,6,4,4,6,4,4,5,5,3,4,6,4,4,3,4,6,6,5,3,6 ,4,5,4,6,5,4,4,3,5,6,4,3,5,5,3,5,4,3,6,4,3,3,3,4,6 ,5,6,3,5,5,6,6,6,5,5,6,5,6,5,4,5,4,4,5,3,6,3,3,6,5 ,6,3,5,3,6,3,5,6,3,4,5,4,3,5,3,5,3,5,3,5,3,5,3,5,5 ,6,
3,3,6,5,5,3,3,4,3,5,6,4,3,3,6,3,6,6,3,4,5,5,5,4 ,4,6,6,3,3,3,5,4,4,3,6,6,5,5,5,4,4,4,5,3,6,3,5,4,5 ,6,3,6,5,3,3,4,5,4,6,3,4,6,3,6,3,4,6,5,3,6,3,5,6,5 ,6,4,5,4,3,6,4,4,3,4,6,3,5,5,3,6,6,6,5,6,6,4,3,6,3 ,4,
4,4,3,6,4,6,4,3,5,4,5,3,4,5,6,6,6,3,4,4,4,4,4,4 ,5,6,4,4,6,6,5,5,5,3,6,3,5,4,6,5,5,4,5,4,5,4,3,3,5 ,4,6,5,5,4,4,6,6,6,3,4,6,6,3,6,5,5,4,6,6,4,3,4,6,3 ,5,6,4,3,5,6,3,4,3,6,6,6,6,3,3,4,4,4,6,6,4,3,6,5,4 ,3,
4,5,3,6,3,4,5,4,4,5,5,3,3,6,6,4,6,4,5,5,3,5,5,5 ,3,6,3,5,4,5,5,6,6,4,4,5,3,3,4,5,5,5,4,6,5,4,5,4,5 ,6,6,3,3,3,4,3,4,6,5,3,5,5,3,3,5,6,3,5,6,3,6,3,5,5 ,5,6,3,6,4,3,4,5,5,3,6,6,6,6,4,5,6,5,3,4,4,3,4,6,3 ,6,
另外需要注意,获取文本框的值始终返回一个字符串,因此以下(min和max)都是字符串
var min = document.getElementById('mindmg').value;
var max = document.getElementById('maxdmg').value;
我会在数学中使用它们之前检查它们实际上是数字,然后再投射它们,比如
var min = +(document.getElementById('mindmg').value);
var max = +(document.getElementById('maxdmg').value);
把+放在前面会产生一个数字如果它可以,如果它不能,它将是NaN(非数字),所以在你使用之前,检查一下他们不是NaN。 Nan很容易被检查,因为没有什么equala Nan甚至不像Nan那样的东西
if(min === min && max === max)
{
//... all good to use con..
}
将检查它们实际上是数字,因为Nan永远不会等于NaN,但3将始终等于3
答案 3 :(得分:0)
<强>演示:强>
你想这样使用它:
var dmg_done = Math.floor(Math.random() * (max - min + 1) + min);
它从0开始的原因是因为Math.random
函数从0到1产生一个浮点数(尽可能多的JS
函数和特征)。
因此,通过告诉它从max - min + 1, ie 4
开始,它避免使用0
作为起始值。
答案 4 :(得分:0)
发现问题。 min和max都用作字符串,而不是数字。所以我只是将它们解析为这样的内容:
var min = parseInt(document.getElementById('mindmg').value);
var max = parseInt(document.getElementById('maxdmg').value);
现在它完美无瑕