- Home /
Question by
3D-Magic-VR · Jan 10, 2012 at 05:48 PM ·
elementsrandomizing
Making a List of objects and randomize the content
Hi every one, I got this little problem:
// How to make this
GameObject[] toChoose = {Object1, Object2, Object3, Object4... Object10};
// In to this
toChoose = {Object5, Object8, Object2, Object9... Object3};
I try using the Random function but does not work to well, any help will be appreciated. Thanks in advance.
Comment
Answer by dannyskim · Jan 10, 2012 at 05:53 PM
Do a google search for Fisher-Yates Shuffle. This should yield you plenty of results for randomizing an array.
Answer by by0log1c · Jan 10, 2012 at 06:05 PM
I'd do something like this, it should theoretically go through each element and assign it somewhere else. Note that there is a possibility that an object does not move.
void RandomizeArray(GameObject[] inputs)
{
for(int i=0;i<inputs.Length;i++)
{
GameObject buffer = inputs[i];
inputs[Random.Range(0,inputs.Length)] = buffer;
}
}
Your answer is not to solid, some times there's double or even triple numbers, but thanks anyway.