Random Range Crash

Hi everyone.

I have to say im stumped with this one.

When using Random.range I get a crash. No errors just a straight spiny wheel of death crash.

When using this in code it is fine:

rand = Random.Range(0, 29);

but when using this I get the crash. note that randomRange is an int set to 29:

rand = Random.Range(0, randomRange);

I imagine its going to be something silly but I cant work it out. Maybe someone else can see something.

Thanks

Random.Range works differently as an int than it does as a float, so if the randomRange variable is a float, even if it’s assigned an int value, it’ll change the actual function being called. This is dangerous being Random.Range(int, int) doesn’t include the “max” number itself, just “up to max” (exclusive), while Random.Range(float, float) (and int/float and float/int by implicit conversion) will include the final number as a max value (inclusive). As it is, I have no way of knowing if you’re using “var” (in Javascript or C#) and implying the value types, but that could be messing you up.

Alternatively, randomRange isn’t the value that you think it is, or you’re using the nullable version “int?”. Try Debug.Logging it just before this statement and make sure. Really can’t be more detailed without seeing more of the script- a single line implies that you know exactly what the error is, and if you knew that, you probably wouldn’t be asking for help ^_^.

2 Likes

Pretty much.

You can always call a random range, then round the output.

1 Like

Hi Lysander & Not_Sure, thanks for the response.

Random range is printing in the console what i expect it to so i think thats correct.

Random range is set to an Int. And rand is also an int. So i assume I fit into your (int/int) scenario.

What i dont understand is why when i type into the code “29” it works but doesnt work if the 29 is stored in an int variable.

I’ve done what I could with what I had- if you want additional assistance you’ll really need to post the rest of the script. As Random.Range is built into Unity and running fine for everyone else, it can’t possibly be the problem here.

Script - CBLevelManager

xmlLoader.randomRange = 29;

Script - XmlLoader

voidrandomQuestionSelection(intguiId) 
 {

int rand;
List<int> usedNums = newList<int>();
for (inti = 0; i < 11; i++)
 {

rand = Random.Range(0, randomRange);
Debug.Log("Agree Disagree");
if(usedNums.Contains(rand))
 {
i--;
 }
else
 {
usedNums.Add(rand);
//agreeDisagree.selectedQuestions.Add(questions[rand]);
switch (guiId)
 {
case1:
Debug.Log("Agree Disagree");
agreeDisagree.selectedQuestions.Add(questions[rand]);
break;
case2:
Debug.Log("");
break;
 }
 }
 }
 }

I hope this helps.

if (usedNums.Contains(rand))
{
   i--;
}

This is not a good way of creating a random sequence, since you could easily waste thousands of iterations. I actually wrote one the other day for somebody though, let me go see if I can find it.

EDIT: Found it.

using System.Linq;

public List<int> RandomRangeList(int start, int count)
{
   List<int> randomList = new List();
   List<int> remaining = Enumerable.Range(start, count).ToList();

   while(remaining.Count > 0)
   {
      int randomIndex = Random.Range(0, remaining.Count);
      randomList.Add(remaining[randomIndex]);
      remaining.RemoveAt(randomIndex);
   }

   return randomList;
}

As in a freeze? This normally indicates an infinite loop of some sort.

Messing with the iterator inside of a for loop is a good way to do that. Your i-- is adding one more iteration to the loop each time it runs. Never modify i inside a for loop.

If the code ever runs with randomRange set to a value of less then 11, you get an infinite loop.

There are much more efficient ways to generate random orders, @DonLoquacious posted a good one

Hi everyone,

Thanks for the advice last week. And thank you @DonLoquacious your code worked brilliantly. I went back over the whole code design to make sure the approach was correct and implemented your random range code and it works better than ever.

Thank you.