Question by
Bauflow · Jul 14, 2020 at 08:52 PM ·
randomrandom.rangefor-loopinfinite
random number without repeating
hello, i created a method who choose a random number, but in some cases generate an infinite loop, i don't know why after repeating Random Range 6 times in the loop, the loop become infinit.
public int initialSceneIndex = 6;
public int lastSceneIndex = 15;
public int[] scenesSelected = new int[] {6,7,8,9,10,11,12,13,14};
public int GetRandomWorldSceneIndex()
{
int u = Random.Range(initialSceneIndex, lastSceneIndex + 1);
int j = 0;
for (int i = 0; i < scenesSelected.Length; i++)
{
j++;
if (j >= 30)
{
Debug.Log("infinite");
break;
}
if (u != scenesSelected[i])
{
continue;
}
else
{
u = Random.Range(initialSceneIndex, lastSceneIndex + 1);
i = -1;
Debug.Log("repeat");
}
}
return u;
}
i think in this case the answer always should be 15, the infinite loop only created when " Debug.Log("repeat")" is more than 6
Comment
Your answer
Follow this Question
Related Questions
Random.Range code problem or problem with Unity? 1 Answer
how to randomly instantiate prefabs but each with it's own probability? 2 Answers
How do I generate a random number in regular time intervals? 0 Answers
What range of values need to be inserted if we need to pull a random item from the list? 0 Answers
Can I generate values outside of the Random.insideUnitCircle?? 1 Answer