# unique list of 10 random numbers

I'm trying to get 10 random numbers from 0 - 120, then store those numbers into a array (or list) only trick is, i keep getting duplicates. is there a good way to get the random number (random.range(0,120) then add it to an array, ONLY if it isn't already in there?

You could have a list of all numbers from 0-120, then remove the ones you've already picked for the array:

``````function Start () {
var numbers = new List.<int>(121);
for (i = 0; i < 121; i++) {
}
var randomNumbers = new int[10];
for (i = 0; i < randomNumbers.Length; i++) {
var thisNumber = Random.Range(0, numbers.Count);
randomNumbers *= numbers[thisNumber];*
*numbers.RemoveAt(thisNumber);*
*}*
*}*
*```*``````

Every time you are adding a new random number to your list you sould go thro all numbers in the list to see whether that number alredy exists in the list. If it exists in the list, you ask for another one, if not you add it to the list.

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 *= randomNumber;*
*}*
*}*
*```*
*<p>with this code you will never have 2 same numbers</p>*
*<p>hope the explanation of the code helps you</p>*``````

Recommend you take a look at Unity Random on the asset store. It has shuffle bags which would fix this. I use it for a similar scenario.