1

I am trying to generate three Random DIFFERENT Numbers at once. But it is not generating them properly. Please help me to solve this.

Following is my code

function makeRand(){
        var numLow = 1,
        numHigh = 7,        
        adjustedHigh = (parseFloat(7) - parseFloat(1)) + 1,        
        numRand = Math.floor(Math.random()*adjustedHigh) + parseFloat(numLow);
        console.log("In makeRand "+numRand);
        chkGen(numRand);
    }
    function chkGen(prev){
        console.log("In chkGen "+prev);
        if(latest===prev){makeRand();}
        old=prev;
        latest=prev;
        console.log("Old is Now "+old);
        return prev;
    }

$(document).ready(function(){
   window.old=0;
   window.latest=0; 
   for(i=0;i<3;i++){
    console.log("Started Here "+old);
    var rnd=chkGen(old);
    imgNo[i] = rnd;
    }
   alert(imgNo);
});
Bharat Soni
  • 2,824
  • 6
  • 23
  • 48
  • 1
    "But it is not generating them properly" - that's useful. care to actually explain. – Mitch Wheat May 08 '13 at 06:39
  • 2
    @MitchWheat Thats why I asked the question sir. It generating 0,0,0. I don't think this question deserve a negative because of that sir. :) – Bharat Soni May 08 '13 at 06:41
  • @Nathan I am already using the Logic to generate the single random number here is the question to generate 3 DIFFERENT Numbers. Dude, at least see the question first. – Bharat Soni May 08 '13 at 06:51
  • @Nathan this is not the same thing i am asking, whats wrong with you. Please remove the duplicate mark please. – Bharat Soni May 08 '13 at 06:54

4 Answers4

1

This function will return an array of unique numbers based on how many you want(amount), the highest you want it to go (max), and the lowest you want it to go (min).

function randomNumbers(amount,max,min){
    var x = 1;
    var numbers = new Array();
    while (x <= amount) {
       a = Math.floor(Math.random() * (max - min + 1)) + min;
       var count=numbers.length;
       var unique = true;
       for(var i=0;i<count;i++)
       {
           if(numbers[i]===a){unique = false;}
       }
       if (unique){
          numbers.push(a);
          x = x+1;
       }
    }
    return numbers;
}
Matthew Graves
  • 3,226
  • 1
  • 17
  • 20
0

Do you looking like this http://jsfiddle.net/YuyNL/69/

var chars = "0123456789";
var string_length = 3;
var num_chars = chars.length;
var result1 = '';
var result2 = '';
var result3 = '';

while(string_length--) {
    result1 += chars[Math.floor(Math.random() * num_chars)];
    result2 += chars[Math.floor(Math.random() * num_chars)];
    result3 += chars[Math.floor(Math.random() * num_chars)];

}
$('body').prepend(result1 + '<br>'+result2 + '<br>'+result3 + '<br>');

If you want you can add characters also and you can increase the length of numbers as well.

vinothini
  • 2,606
  • 4
  • 27
  • 42
0

jQuery has a useful method if you want to check for duplicates: jQuery.inArray()

function makeRand() {
    var numLow = 1,
        numHigh = 7,
        adjustedHigh = (parseFloat(7) - parseFloat(1)) + 1,
        numRand = Math.floor(Math.random() * adjustedHigh) + parseFloat(numLow);

    return numRand;
}

$(document).ready(function () {
    imgNo = new Array();
    while(imgNo.length < 3) {
        var rnd = makeRand();
        if($.inArray(rnd, imgNo) >= 0) continue;
        imgNo.push(rnd);
    }
    alert(imgNo);
});

http://jsfiddle.net/samliew/E4QkQ/

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
0

Try this:

function generate_three_random_numbers()
{
    var random_numbers = [];

    while(true) {
        // generate random number
        var num = Math.floor(Math.random()*((parseFloat(7) - parseFloat(1)) + 1)) + parseFloat(1);
        if ($.inArray(num, random_numbers) === -1) {
            random_numbers.push(num);
        }

        if (random_numbers.length === 3) {
            break;
        }
    }

    return random_numbers;
}
vher2
  • 980
  • 6
  • 9