Hy guiz.
Ok Rand1 displays 0 1 and 2 and i need Rand2 to no display Rand 1 number, omit that number
var Rand1 = Math.floor(Math.random()*3);
var Rand2 = Math.floor(Math.random()*3);
Homework ?
var Rand1 = Math.floor(Math.random()*3);
var Rand2 = Math.floor(Math.random()*2);
if (Rand2 == Rand1) Rand2 = 2;
Rand1 Rand2
0 0=>2
0 1
1 0
1 1=>2
2 0
2 1
then distribution is ok
other solution for Rand2 could be:
var Rand2 = (Rand1 + 1 + Math.floor(Math.random()*2)) % 3;
Math.floor(Math.random()*3) and Math.floor(Math.random()*2) evaluate to 1var Rand1 = Math.floor(Math.random()*3);
var Rand2 = Rand1;
while ( Rand2 == Rand1 ){
var Rand2 = Math.floor(Math.random()*3);
}
I understand that English might not be your main language but please at least use a spell checker before submitting your question.
I cannot comment on @benoit's solution but it does not output an even statistical distribution of values for Rand2:
P( Rand2=0 ) = 2/6
P( Rand2=1 ) = 3/6
P( Rand2=2 ) = 1/6
If you want to let Rand2 do its math again (if its the same as rand1), just put the Rand2 in an if-statement.
Adjusted Benoît's code a little.
var Rand1 = Math.floor(Math.random()*3);
var Rand2 = Math.floor(Math.random()*2);
if (Rand2 == Rand1) {
var Rand2 = Math.floor(Math.random()*2);
};
This way it will do the calculation again if Rand2 is the same as Rand1.
Rand2 = Rand1 == Rand2 ? "omited" : Rand2;