Hello,
I am making survey app in Unity. But have problem with function that are generating numbers from 0 to l-1 in random order, while storing them into list questionOrder.
Problem occurs (Unity freezing on play) when I put one specific condition in if statement bellow.
int l = *someList*.Count; // Length I want my new integer list (questionOrder) to be
int i = 0; // current length of *questionOrder* list
while (i < l)
{
int r = Random.Range(0, l - 1);
int k = 0;
while (i > 0 && k < questionOrder.Count && r != questionOrder[k])
{
k++;
}
if (i == 0 || k == questionOrder.Count) // *k < questionOrder.Count* is problematic
{
questionOrder.Add(r);
i++;
}
}
First I am generating random number r, then with ‘nested while’ loop checking if generated number exists, if not ‘nested while’ will end because k == questionOrder.Count (r is compared with every list member, that means r is unique).
Then, in IF statement I am checking what was the reason ‘nested while’ ended (if k == questionOrder.Count then r is unique and will be added to questionOrder list).
If i remove k == questionOrder.Count and set i == 0 to i >= 0 (r does not need to be unique)
it works fine.
Note: i > 0 in ‘nested while’ and i==0 in if are just to work when the questionOrder list is empty.