Attempt at re-arranging list results in crash

I have a more complicated script that I’ve been working on. It’s designed to organize the answers(dic[x,2-5]) randomly to all the questions available. It also keeps track of the correct answer(dicnum) and the secondary answer(splitnum) which is used to make a 50/50 function.

However, for some odd reason either a loop is created or some other unknown error appears and causes Unity to crash.

{for (int currentQuestion = 0; currentQuestion < 10; currentQuestion++) {

a = 0;
b = 0;
c = 0;

for (int j = 2; j<6; j++) {

t = (int)UnityEngine.Random.Range (2, 6);

if (t == j || t == a || t == b || t == c)

while (t == j || t == a || t == b || t == c)

{t = (int)UnityEngine.Random.Range (2, 6);}
							
variable = QuestionData.dic [currentQuestion, j];

QuestionData.dic [currentQuestion, j] = QuestionData.dic [currentQuestion, t];

QuestionData.dic [currentQuestion, t] = variable;

if (QuestionData.dicnum [currentQuestion] == (j - 1))

QuestionData.dicnum [currentQuestion] = t - 1;

if (QuestionData.splitnum [currentQuestion] == (j - 1))

QuestionData.splitnum [currentQuestion] = t - 1;

if (QuestionData.dicnum [currentQuestion] == (t - 1))

    QuestionData.dicnum [currentQuestion] = j - 1;

    if (QuestionData.splitnum [currentQuestion] == (t - 1))

QuestionData.splitnum [currentQuestion] = j - 1;

c = b;
b = a;
a = t;

}}}

I’ve tried commenting out the if statements toward the end, which are used in order to transfer the values for the correct and secondary answers. Unity becomes functional when I comment this out, but it doesnt keep track of the correct answer anymore. Commenting out the while, which is used to ensure that no random number is selected twice, has similar results.

Anyone have any idea what is causing the crash? Any advice on how to fix it?

Found it!

Random.Range has two versions, int and float. When calling the int version (when you do Random.Range(2,6)) the upper bound is exclusive, so that means it will never return 6. Therefore, the program gets stuck in an infinite loop when a,b,c, j= (2,3,4,5) or because there’s no value available for t in the range [2,6)

To fix it, either change your ranges to Random.Range(2,7) OR change it to Random.Range(2.0f, 6.0f); as the floating point version is inclusive.

My suggestion is to use the first, because then you don’t have to 1.) cast a float to an int and 2.) deal with any issues arising from comparing floating point equality.