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

7 Answers 7

2

You almost got it right. The problem is you still multiply the random generated number by the whole amount, so you performed 7 + a random number between 1 and 12 (= max 19) while you needed 7 + a random number between 1 and 6 so the right code for the number calculation is:

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

Edit: Not sure if you need a number between 1 and 6 or 1 and 5, sorry about that.

Sign up to request clarification or add additional context in comments.

Comments

2

I guess you need this:

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

updated the fiddle http://jsfiddle.net/bxZ9G/8/

Comments

1

Looks like your math is wrong. Change it like so:

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

Comments

1

One way to generate a pseudo-random number between min and max:

function rand(min, max) {
    return Math.floor(min + (Math.random() * max));
}

Comments

1

Abstract equation for random number in specified range should be

Min + (int)(Math.random() * ((Max - Min) + 1))

So in your case your code might look like

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

Here's the fixed fiddle: http://jsfiddle.net/bxZ9G/6/

Comments

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

You just need a random number between 0 to 5. You can add that to 7,13,19,25 to get random numbers in the range you need.

Comments

1

You can use the following function:

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

The function was taken from Mozilla's page on Math.random. Use Math.floor to make it a round number.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.