- Home /
Question by
alejandroperez · Mar 06, 2013 at 09:25 PM ·
arrayrandom
¿how to take all values from an array without order?
I would like to take all my values in an array, taking them without order, due to paste them into position in my screen;
I am trying to do a match pair game and I want to have the array values without any order, but I want to take all of them.
¿anybody can help me?
sorry for mistakes
Comment
Answer by AlwaysSunny · Mar 06, 2013 at 09:35 PM
Take a look at this code to see if it's helpful. It's meant to shuffle the elements of an array randomly. If you don't want them shuffled in the source array, just copy them to a new array first. Oh, and replace (string[] texts) with (yourObjectType[] foo), unless they're already strings.
void reshuffle(string[] texts)
{
// Knuth shuffle algorithm :: courtesy of Wikipedia :)
for (int t = 0; t < texts.Length; t++ )
{
string tmp = texts[t];
int r = Random.Range(t, texts.Length);
texts[t] = texts[r];
texts[r] = tmp;
}
}