You can fill an array with 4 random numbers like this: Code (csharp): 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: Code (csharp): 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: Code (csharp): var rands = HashSet<int>(); while (rands.Count < 100) rands.Add(Random.Range(0, 1000);
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. Code (csharp): 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.
HashSet<T> doesn't provide an indexer but you can copy it to an array after you fill it up. Code (csharp): HashSet<int> h = new HashSet<int>(); int[] a; while (h.Count < 100) { h.Add(Random.Range(0, 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. Code (csharp): 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... Code (csharp): // // 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 Code (csharp): // // 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 Code (csharp): 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++) { IntList.Add(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); } } }