Vignesh Pichamani
Vignesh Pichamani

Reputation: 8070

Randomly Generating Number using javascript

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

Upvotes: 1

Views: 150

Answers (7)

Lidor
Lidor

Reputation: 394

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.

Upvotes: 1

Gopi Krishna
Gopi Krishna

Reputation: 494

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/

Upvotes: 2

Praveen B
Praveen B

Reputation: 207

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.

Upvotes: 1

matewka
matewka

Reputation: 10168

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/

Upvotes: 1

tobi
tobi

Reputation: 789

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

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

Upvotes: 1

Coded Monkey
Coded Monkey

Reputation: 605

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.

Upvotes: 2

neelsg
neelsg

Reputation: 4842

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

Upvotes: 1

Related Questions