Pretty new to coding in general here, I am trying to pre-select a random order for the use of random level selection. For example, 10 levels pre-selected into random order :
All you have to do is to put a random number in each of your array spot. You can clamp the random values to the bounds you need.
If you have a lot of values to put in your array (you said over 100), then create those values in a loop and fill in your array at the same time.
Ok then it is the index in the array that may be randomized
Of course, doing so you’ll have to check that there is nothing already in the array when you get a new index.
And do some little optimization the more the array is filled in.
You have your array of indices, 0…n-1. Pick one of these at random, and add it to your randomized list. Move the last number from the index array to the picked number’s spot, reducing the size of the index array to n-1. Repeat.
This runs in O(n) time, and I think it’s as fast and as simple as you can do it.
That’s pretty much the same as what I wrote, but List.RemoveAt runs in O(n) time, so your method would run in O(n^2) time. That doesn’t matter if your n is small, or if it doesn’t get run that often, but in this case, the small difference between our algorithms has a large theoretical performance impact.
Something like this:
while(objects.Length > 0) {
int index = Random.Range(0, objects.Length);
int lastIndex = objects.Length - 1;
randomizedObjects.Add(object[index]);
objects[index] = objects[lastIndex];
objects[lastIndex] = null;
}