0

I'm trying to make sure two randomly generated numbers don't match, is there a way to do this in an if-statement?


i'm fairly new to javascript and couldn't find anything that answered my question, rand2 and rand3 both pick from the same list and it would make the outcome seem odd if they matched. my code also stopped working when i put the if-statement in (if i messed that up i guess it wouldn't matter if i got an answer).

      function sentence() {
        var rand1 = Math.floor(Math.random() * 2);
        var rand2 = Math.floor(Math.random() * 12);
        var rand3 = Math.floor(Math.random() * 12);
        var rand4 = Math.floor(Math.random() * 10);
        var rand5 = Math.floor(Math.random() * 10);
        var rand6 = Math.floor(Math.random() * 10);
        //                var randCol = [rand1,rand2,rand3,rand4,rand5];
        //                var i = randGen();

        if (rand2 == rand3) {
        // whatever changes one
        } else {
        //
        }
Isaac
  • 3
  • 1
  • See [Random number, which is not equal to the previous number](https://stackoverflow.com/q/40056297/), [run for random numbers and keep state](https://stackoverflow.com/q/41001101/) – guest271314 Jan 31 '19 at 05:11

1 Answers1

0

You could use a do while statement that runs while the numbers are the same, therefore will terminate as soon as the random numbers generated are different.

e.g.

var rand2 = 1;
var rand3 = 1;

do {
    rand2 = Math.floor(Math.random() * 12);
    rand3 = Math.floor(Math.random() * 12);
} while(rand2 === rand3 );

Hope that helps :)

sjdm
  • 591
  • 4
  • 10