I am trying to shuffle strings stored in an array so that they are in a pseudo-random order where a string is not repeated twice in a row. The array contains 5 different strings repeated 5 times each. I have been unable to find any code that does this, only randomization code that can allow for presentation of a string twice in a row. Any suggestions on how to go about this?
You could use Mathf.random(0,string.length) for the random part and an if statement for filtering out already user elements
One possibility
public string [] _WordsToRandomize = new string[]{"Foo", "Bar", "Word", "Unity", "Answers"};
public string [] _Randomized;
void Start ()
{
_Randomized = new string[30];
// take random index from words array
int rndm = Random.Range (0, _WordsToRandomize.Length);
// loop through array to fill
int i = _Randomized.Length;
while (--i >= 0)
{
// take the random word from words array
string rndmWord = _WordsToRandomize[rndm];
_Randomized *= rndmWord; // put it in target array*
-
// take the first word in the words-array*
-
string firstWord = _WordsToRandomize[0];*
-
_WordsToRandomize[0] = rndmWord; // put the used word as the first in array*
-
_WordsToRandomize[rndm] = firstWord; // ... and the first word replaces the previous random word*
-
// take a new random index excluding index 0 that has the previous word*
-
rndm = Random.Range (1, _WordsToRandomize.Length);*
-
}*
-
}*