Quantcast
Channel: Answers for "unique list of 10 random numbers"
Viewing all articles
Browse latest Browse all 8

Answer by poncho

$
0
0

this is an other way to doit, my code suggestion would be this

void Start()
{
   //having an int Array of 10 members
   int [] numbers = new int[10];
   //using a string variable to keep record of the used numbers
   string usedNumbers ="-";
   //cycle to get 10 random numbers
   for (int i = 0 ; i<10; i++)
   {
      //get a random number between 0 - 120, 0 and 120 included on the random numbers
      int randomNumber = Random.Range(0,121);
      //Checking if the random number you get is unique, if you already have it in the string means that this random number is repeated, the "-X-" is to make the difference between the numbers, like -1- from -11-, if you dont have the "-X-" the 1 is in 21 and would be a "repeated" number
      while(usedNumbers.Contains("-"+randomNumber.ToString()+"-"))
      {
         //if a number is repeated, then get a new random number
         randomNumber = Random.Range(0,121);
      }
      usedNumbers+= randomNumber.ToString()+"-";
      numbers[i] = randomNumber;
   }
}

with this code you will never have 2 same numbers

hope the explanation of the code helps you


Viewing all articles
Browse latest Browse all 8

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>