Fill array with random integers

You can fill an array with 4 random numbers like this:

``````arr[0] = Random.Range (0,10);
arr[1] = Random.Range (0,10);
arr[2] = Random.Range (0,10);
arr[3] = Random.Range (0,10);
``````

If you want to have different random numbers in the array, you can do this:

``````arr[0] = Random.Range (0,10);
do {
arr[1] = Random.Range (0,10);
} while (arr[1] == arr[0]);

do {
arr[2] = Random.Range (0,10);
} while ((arr[2] == arr[1]) || (arr[2] == arr[0]));

do {
arr[3] = Random.Range (0,10);
} while ((arr[3] == arr[2]) || (arr[3] == arr[1]) || (arr[3] == arr[0]));
``````

But this is not the way when you have an array with 100 random numbers.
For a long time Iâ€™m thinking about how to do this in a better way.
Does anybody know a better way to fill an array with different random numbers?

If what you want is to fill a collection with a hundred unique random numbers, then you can use a HashSet of integers like so:

``````var rands = HashSet<int>();
while (rands.Count < 100)
``````
``````for ( var iter : int = 0; iter < 100; iter++ ) {
arr[iter] = Random.Range(0, 101);
}
``````

Yes, I need a collection with unique random numbers.
HashSet is new for me.
Itâ€™s not clear for me right now how it works. I see you create a list with unique random numbers but how can you select one specific random number from the list?

Amon:
In your code you donâ€™t get unique numbers and thatâ€™s what I need. But thanks for reply.

The best way is to insert the numbers into an array in order then shuffle the array. You do not need to use Random.Range until shuffeling. This will provide unique numbers in each array element, but shuffled so itâ€™s random.

``````    private int[] intArray = new int[100];

private void RandomUnique()
{
for (int i = 0; i < 100; i++)
{
intArray[i] = i;
}
Shuffle(intArray);
}

public void Shuffle(int[] obj)
{
for (int i = 0; i < obj.Length; i++)
{
int temp = obj[i];
int objIndex = Random.Range(0, obj.Length);
obj[i] = obj[objIndex];
obj[objIndex] = temp;
}
}
``````

You did not really state what sort of number range you were looking at so i assumed 0-99.

1 Like

A simple solution.

I think shuffle the array would be the best way for me. Never thought of that.
Thanks.

http://msdn.microsoft.com/en-us/library/system.linq.enumerable.range.aspx

HashSet doesnâ€™t provide an indexer but you can copy it to an array after you fill it up.

``````HashSet<int> h = new HashSet<int>();
int[] a;
while (h.Count < 100)
{
}
h.CopyTo(a);
for (var i = 0; i < a.Length; i++)
{
Debug.Log(a[i]);
}
``````

But, EliteMossyâ€™s solution is still the better one.

EliteMossyâ€™s technique is known as the Knuth-Fisher-Yates Shuffle. Very useful for randomizing arrays of any type.

Building on EliteMossyâ€™s implementation, I wrote a quick generalized C# template that I use in our code. You can still pass in integer arrays, but any other type of array as well. Perhaps, for example, an array of Gamebject prefabs to shuffle the order in which they spawn.

``````public static void RandomizeArray<T>(T[] array)
{
int size = array.Length;
for (int i=0; i < size; i++)
{
int indexToSwap = Random.Range(i, size);
T swapValue = array[i];
array[i] = array[indexToSwap];
array[indexToSwap] = swapValue;
}
}
``````

To call it with integersâ€¦

``````//
// Non repeating random numbers from 0 to count
//

int[] indexArray = new int[count];

// Fill with sequeential indexes then randomize
for(int i=0; i<indexArray.Length; ++i)
indexArray[i] = i;

RandomizeArray<int>(indexArray);
``````

To call it with game object prefabs

``````//
// Non repeating prefab spawn
//
public GameObject arrayOfPrefabsToSpawn[]; // An array of the prefabs that should be spawned

RandomizeArray<GameObject >(arrayOfPrefabsToSpawn);

// Now just sequentially instantiate the prefab gameobjects in the arrayOfPrefabsToSpawn as needed.
// You can, of course, just call RandomizeArray again when the end is reached and start the spawn loop again.
for(int i=0; i<arrayOfPrefabsToSpawn.Length; ++i)
{
GameObject.Instatiate(arrayOfPrefabsToSpawn[i]);
yield return new WaitForSeconds(spawnDelay);
}
``````

Iâ€™ve come up to this after few tries and it seems to work nice and well and it gives truly random unique numbers. Just test it and tell me if it works for you

``````public static class ShuffleLike
{
public static int[] arr = new int[100];
public static Random R = new Random();
public static int Aint;
public static List<int> IntList= new List<int>();

public static void FillList()       //Static method to call
{
for (int i = 1; i < 101; i++)
{
}
FillArray();
}

static void FillArray()
{
for (int i = 0; i < 100; i++)
{
Aint = R.Next(0, IntList.Count);
arr[i] = IntList[Aint];
IntList.RemoveAt(Aint);
}
}
}
``````