使用Javascript获取2个数字之间的随机数,但不太可能具有特定范围
我用一个随机数函数得到219到1500之间的两个随机数。这与预期一样有效,但我希望1200到1500之间的数字发生的可能性减少50%。我怎样才能做到这一点 我确实尝试过先搜索,但随机数太多了,很难找到与我的问题相关的任何东西使用Javascript获取2个数字之间的随机数,但不太可能具有特定范围,javascript,math,random,Javascript,Math,Random,我用一个随机数函数得到219到1500之间的两个随机数。这与预期一样有效,但我希望1200到1500之间的数字发生的可能性减少50%。我怎样才能做到这一点 我确实尝试过先搜索,但随机数太多了,很难找到与我的问题相关的任何东西 function getRandomBase (min, max) { orBase = Math.floor(Math.random() * (max - min + 1)) + min; document.getElementById("box1
function getRandomBase (min, max) {
orBase = Math.floor(Math.random() * (max - min + 1)) + min;
document.getElementById("box1").value = orBase;
newBase = Math.floor(Math.random() * (max - min + 1)) + min;
document.getElementById("box3").value = newBase;
}
我想试试这样的方法:
function number() {
var isLower = (Math.random() * 3) < 2;
if(isLower) {
return randomNumber(219,1199);
} else {
return randomNumber(1200,1500);
}
}
function randomNumber(min,max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
函数号(){
var isLower=(Math.random()*3)<2;
如果(岛下){
返回随机数(2191199);
}否则{
返回随机数(12001500);
}
}
函数随机数(最小值、最大值){
返回Math.floor(Math.random()*(max-min+1))+min;
}
更新的fiddle:50%的可能性低于正常值,或者50%的可能性低于210-1200范围内的数字?使用随机数选择使用的随机数方法。。。无价的优雅的解决方案,非常感谢!