我尝试组合6 with 5
次。从1到6,我想要随机生成7 to 12
和13 to 18
至25 to 30
。
我尝试过像
setInterval(function() {
var number = 1 + Math.floor(Math.random() * 6); //i tried from 1 to 6
$('#my_div1').text(number);
var number = 7 + Math.floor(Math.random() * 12); // i tried from 7 to 12
$('#my_div2').text(number);
var number = 13 + Math.floor(Math.random() * 18); // from 13 to 18
$('#my_div3').text(number);
var number = 19 + Math.floor(Math.random() * 24); //from 19 to 24
$('#my_div4').text(number);
var number = 25 + Math.floor(Math.random() * 30); // from 25 to 30
$('#my_div5').text(number);
},
1000);
对于第一个本身正在为下一个工作我没有得到适当的价值。我之前在php中使用rand(1,5);
就是这样尝试但是我不知道如何在javascript中执行此操作。任何建议都会很棒。
以下是 Fiddle
答案 0 :(得分:2)
你几乎做对了。问题是您仍然将随机生成的数字乘以整数,因此您在需要7 + a random number between 1 and 12
时执行7 + a random number between 1 and 6
(=最大19),因此数字计算的正确代码为:
var number = 7 + Math.floor(Math.random() * 6);
var number = 13 + Math.floor(Math.random() * 6);
var number = 19 + Math.floor(Math.random() * 6);
var number = 25 + Math.floor(Math.random() * 6);
编辑:不确定您是否需要1到6或1和5之间的数字,对不起。
答案 1 :(得分:2)
我想你需要这个:
setInterval(function() {
var number = 1 + Math.floor(Math.random() * 6); //i tried from 1 to 6
$('#my_div1').text(number);
var number = 7 + Math.floor(Math.random() * 6); // i tried from 7 to 12
$('#my_div2').text(number);
var number = 13 + Math.floor(Math.random() * 6); // from 13 to 18
$('#my_div3').text(number);
var number = 19 + Math.floor(Math.random() * 6); //from 19 to 24
$('#my_div4').text(number);
var number = 25 + Math.floor(Math.random() * 6); // from 25 to 30
$('#my_div5').text(number);
},
1000);
更新了小提琴 http://jsfiddle.net/bxZ9G/8/
答案 2 :(得分:1)
看起来你的数学错了。像这样改变:
setInterval(function() {
var number = 1 + Math.floor(Math.random() * 6); //i tried from 1 to 6
$('#my_div1').text(number);
var number = 7 + Math.floor(Math.random() * 6); // i tried from 7 to 12
$('#my_div2').text(number);
var number = 13 + Math.floor(Math.random() * 6); // from 13 to 18
$('#my_div3').text(number);
var number = 19 + Math.floor(Math.random() * 6); //from 19 to 24
$('#my_div4').text(number);
var number = 25 + Math.floor(Math.random() * 6); // from 25 to 30
$('#my_div5').text(number);
},
1000);
答案 3 :(得分:1)
在 min 和 max 之间生成伪随机数的一种方法:
function rand(min, max) {
return Math.floor(min + (Math.random() * max));
}
答案 4 :(得分:1)
您可以使用以下功能:
// Returns a random number between min and max
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
该函数取自Math.random上的Mozilla's page。
使用Math.floor
将其设为一个整数。
答案 5 :(得分:1)
指定范围内随机数的抽象方程应为
Min +(int)(Math.random()*((Max - Min)+ 1))
因此,在您的情况下,您的代码可能看起来像
function getRandomRange(min, max) {
return min + Math.floor(Math.random() * ((max - min) + 1));
}
setInterval(function() {
var number = getRandomRange(1, 6); //i tried from 1 to 6
$('#my_div1').text(number);
var number = getRandomRange(7, 12); // i tried from 7 to 12
$('#my_div2').text(number);
var number = getRandomRange(13, 18); // from 13 to 18
$('#my_div3').text(number);
var number = getRandomRange(19, 24); //from 19 to 24
$('#my_div4').text(number);
var number = getRandomRange(25, 30); // from 25 to 30
$('#my_div5').text(number);
}, 1000);
这是固定的小提琴: http://jsfiddle.net/bxZ9G/6/
答案 6 :(得分:1)
setInterval(function() {
var number = 1 + Math.floor(Math.random() * 6); //i tried from 1 to 6
$('#my_div1').text(number);
var number = 7 + Math.floor(Math.random() * 6); // i tried from 7 to 12
$('#my_div2').text(number);
var number = 13 + Math.floor(Math.random() * 6); // from 13 to 18
$('#my_div3').text(number);
var number = 19 + Math.floor(Math.random() * 6); //from 19 to 24
$('#my_div4').text(number);
var number = 25 + Math.floor(Math.random() * 6); // from 25 to 30
$('#my_div5').text(number);
},
1000);
您只需要一个介于0到5之间的随机数。您可以将其添加到7,13,19,25以获得所需范围内的随机数。