Skip to content Skip to sidebar Skip to footer

How To Generate Two Different Random Numbers?

I need to generate two different random numbers, they can't be equal to each other or to a third number. I tried to use a lot of if's to cover every possibility but, it seems my al

Solution 1:

You can run a while loop until all numbers are different.

// All numbers are equalvar numberOne = 3; 
var numberTwo = 3; 
var numberThree = 3; 

// run this loop until numberOne is different than numberThreedo {
    numberOne = Math.floor(Math.random() * 4);
} while(numberOne === numberThree);

// run this loop until numberTwo is different than numberThree and numberOnedo {
    numberTwo = Math.floor(Math.random() * 4);
} while(numberTwo === numberThree || numberTwo === numberOne);

Here is the jsfiddle with the above code based on @jfriend00's suggestion http://jsfiddle.net/x4g4kkwc/1.

Here is the original working demo: http://jsfiddle.net/x4g4kkwc/

Solution 2:

You can create an array of random possibilities and then remove items from that array as they are used, selecting future random numbers from the remaining values in the array. This avoids looping trying to find a value that doesn't match previous items.

functionmakeRandoms(notThis) {
    var randoms = [0,1,2,3];

    // faster way to remove an array item when you don't care about array orderfunctionremoveArrayItem(i) {
        var val = randoms.pop();
        if (i < randoms.length) {
            randoms[i] = val;
        }
    }

    functionmakeRandom() {
        var rand = randoms[Math.floor(Math.random() * randoms.length)];
        removeArrayItem(rand);
        return rand;
    }

    // remove the notThis item from the arrayif (notThis < randoms.length) {
        removeArrayItem(notThis);
    }

    return {r1: makeRandom(), r2: makeRandom()};
}

Working demo: http://jsfiddle.net/jfriend00/vhy6jxja/

FYI, this technique is generally more efficient than looping until you get something new when you are asking to randomly select most of the numbers within a range because this just eliminates previously used numbers from the random set so it doesn't have to keep guessing over and over until it gets an unused value.

Solution 3:

This version minimizes the number of calls to random like you did, but is a bit simpler and not biased. In your version, there is a 2/4 chance that numberOne goes to 0, and a 1/4 chance if goes to 1 and 2. In my version there are equal odds of numberOne ending up as 0, 1 or 2).

i0 = Math.floor(Math.random() * 4); //one of the 4 numbers in [0, 4), namely 3i1 = Math.floor(Math.random() * 3); //only 3 possibilities left nowi2 = Math.floor(Math.random() * 2); //only two possibilities left nowx0 = i0;x1 = i1 + (i1 >= i0 ? 1 : 0);x2 = i2 + (i2 >= i0 ? 1 : 0) + (i2 >= i1 ? 1 : 0);

Its a special case of the array-shuffling version deceze mentioned but for when you have only two numbers

Solution 4:

I'm not sure of what you're trying to do (or actually, why is your code so complicated for what I understood). It might not be the most optimized code ever, but here is my try :

var n3 = 3;
var n2 = Math.floor(Math.random() * 4);
var n1 = Math.floor(Math.random() * 4);

while(n1 == n3)
{
    n1 = Math.floor(Math.random() * 4);
}
while (n2 == n1 || n2 == n3)
{
    n2 = Math.floor(Math.random() * 4);
}

EDIT : Damn, too late ^^

Solution 5:

var n = 4; //to get two random numbers between 0 and 3var n3 = 2; //for examplevar n1 = Math.floor(Math.random(n-1));
var n2 = Math.floor(Math.random(n-2));
if(n1 >= n3) {
    n1++;
    if(n2 >= n3)
        n2++;
    if(n2 >= n1)
        n2++;
} else {
    if(n2 >= n1)
        n2++;
    if(n2 >= n3)
        n2++;
}

You need to compare n2 with the minimum of n1 and n3 first to ensure you do not have an equality:

Suppose n1=1 and n3=2. If you get n2=1 and compare it first with n3, you won't increase n2 in the first step. In the second step, you would increase it since n2 >= n1. In the end, n2 = 2 = n3.

This algorithm guarantees to have a uniform distribution, and you only call twice Math.random().

Post a Comment for "How To Generate Two Different Random Numbers?"