1

I tried to make the combination of 6 with 5 times. Like from 1 to 6 I want to generate randomly similarly 7 to 12 and 13 to 18 upto 25 to 30. I tried this like

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);

For the first one itself is working for the next i didn't get proper value. I tried this previously in php with rand(1,5); like that But i am not sure how to do this in javascript. Any Suggestion would be great.

Here is the Fiddle

4

7 回答 7

2

你几乎做对了。问题是您仍然将随机生成的数字乘以总量,因此您在7 + a random number between 1 and 12需要时执行了(= max 19),7 + a random number between 1 and 6因此数字计算的正确代码是:

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 之间的数字,对此感到抱歉。

于 2013-10-17T12:10:13.887 回答
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/

于 2013-10-17T12:10:38.403 回答
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 以获得所需范围内的随机数。

于 2013-10-17T12:14:10.283 回答
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);
于 2013-10-17T12:10:04.433 回答
1

在minmax之间生成伪随机数的一种方法:

function rand(min, max) {
    return Math.floor(min + (Math.random() * max));
}
于 2013-10-17T12:10:34.567 回答
1

您可以使用以下功能:

// Returns a random number between min and max
function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}

该函数取自Mozilla在 Math.random 上的页面。用于Math.floor使其成为整数。

于 2013-10-17T12:10:49.823 回答
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/

于 2013-10-17T12:12:51.403 回答