Array List unique random numbers

Hello I have been searching on the site for a couple of hours and haven’t figured it out. All I want to do is the screen to print 10 random numbers from 1-20 without repeats. I tried to use the “list” and that gave errors as did everything else I tried. And all I am comfortable with is javascript.

Use a shuffle bag.

Duplicate question kindly follow the link for more info i was having the same problem and it’s solved…make sure you check it and don’t forget to Tick mark the answer if Helped…Cheers … Enjoy…

link text

-edit-

A much better method would be to add them to a hashtable and then convert the keys or values into whatever data structure you want, in this case an array. You might not even have to do that because you could put the printing code in the foreach loop instead of copying the values into an array.

`

public int[] GenerateUniqueRandom(int amount, int min, int max)
{
	Hashtable h = new Hashtable();
	for (int i = 0; i < amount; i++)
	{
		bool done = false;
		while (!done)
		{
			int num = Random.Range(min, max);
			if (!h.ContainsKey(num))
			{
				h.Add(num,num);
				done = true;
			}
		}
	}
	int[] arr = new int[amount];
	int it = 0;
	foreach (DictionaryEntry e in h)
	{
		arr[it] = (int)e.Key; //or e.Value, doesnt matter, both the same
		it++;
	}
	return arr;
}

`

-old-

My solution would be to add a random number, then try to add another number and if it already exists cancel and try again.

C#
`

public int[] GenerateUniqueRandom(int amount, int min, int max)
{
    int[] arr = new int[amount];

    for (int i = 0; i < amount; i++)
    {
        bool done = false;
		while (!done)
		{
			int num = Random.Range(min, max);

			int j = 0;
			for (j = 0; j < i; j++)
			{
				if (num == arr[j])
				{
					break;	
				}
			}

			if (j == i)
			{
				arr *= num;*
  •   			done = true;*
    
  •   		}*
    
  •   	}*
    

}

  •   return arr;*
    

}
`
Then just call GenerateUniqueRandom(10, 1, 20) to get your array of random unique values.