mercredi 8 mars 2017

Random Number no reapteating value in a row

I have the following random number gen:

val = rnd.Next(1, 4)+1; 

I have looked at simlar questions on the site and I believe the +1 is meant to prevent getting the same number in a row.

I want the sequence to be random, but I do not want the same value twice in a row. For example:

1,1,2,2,3,4 would be wrong but 1,2,1,2,3,4,3 would meet my needs.

I tried the following but it doesnt solve my issue:

void numLogic()
    {
      int val;
      int oldval = val;
      val = rnd.Next(1, 4)+1;
      while (oldval == val)
            {
                val = rnd.Next(1, 4) + 1;
            }
     }

Note that numLogic() is called successfully after each number is generated by an object collision in unity. The problem is not with the game object, this works fine. I just don't want two number the same in a row.




Aucun commentaire:

Enregistrer un commentaire