0

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);
2
  • What's the logic behind your question? Rand2 = Rand1 == Rand2 ? "omited" : Rand2; Commented Nov 22, 2011 at 15:24
  • I just need Rand2 to skip the Rand1 privided number. For example Rand1 gives 2 and i need Rand2 to provide either 0 or 1 but never 2 Commented Nov 22, 2011 at 15:29

3 Answers 3

2

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;
Sign up to request clarification or add additional context in comments.

5 Comments

If Rand1 is 3 and Rand2 is 3, you will jump over to 4 which is out of the range. You will probably want to do some sort of modular math.
No i just need the rezult from Rand2 to skip the rezult from Rand1. For example Rand1 gives 2 and i need Rand2 to provide either 0 or 1 but never 2
@Jose : No Rand2 can not be 3 because it is Rand*2 (not 3)
@Radu : it is the case in my example
These lines of code produce a different random number distribution for Rand2: Rand2 only is set to 2 if Math.floor(Math.random()*3) and Math.floor(Math.random()*2) evaluate to 1
1
var 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

1 Comment

Ok, I have change my solution. Your solution if right but while could be infinite loop (statistically). Time execution is not defined
0

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.

1 Comment

the second time, Rand2 could be same as Rand1 !

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.