Random.Range() always returns the exact same value

In my code there is a function that uses Random.Range() and it always returns the same value.

There are at least a dozen other Random.Range() in my code, but this one just refuses to work. Any assistance would be appreciated.

using Random = UnityEngine.Random;
private int PossibleSum;
private int[] ButtonType = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
private int[] TypeWeights = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
private bool[] Possible     = { false, false, false, false, false, false, false, false, false };

private int FindMeButton()
    {
        int weight = 0;
        //The line below gets called 3-5 times each time the code runs and it always picks the first or last value of the given bounds and repeats it for those 3-5 calls.
        int number = Random.Range(1, PossibleSum + 1);
        for (int i = 0; i < 9; i++)
        {
            if (Possible[i] == true)
            {
                weight += TypeWeights[ButtonType[i]];
                if (number <= weight) { return i; }
            }
        }
        Debug.Log("huh?");
        return -1;
    }

Do you ever assign a value to PossibleSum?

Yes, it ranges from 2-6 and there is an if(PossibleSum==0){return;} just before this fragment

Did you try Debug.Log(number + " is a random value between 1 and " + (PossibleSum + 1));?

Yeah, it prints
1 is a random value between 1 and 4
1 is a random value between 1 and 4
1 is a random value between 1 and 4
when I ran it again it was
3 is a random value between 1 and 6
3 is a random value between 1 and 6
3 is a random value between 1 and 6
3 is a random value between 1 and 6
3 is a random value between 1 and 6
and it continues in a similar pattern

Are you perhaps setting Random.state somewhere? That’d cause the random generator to reset, and if you have the exact same sequence of calls after that you’ll get the exact same sequence of results.

1 Like

There was a bug where some of the code for 2D stuff in URP didn’t restore the random state after using a fixed seed.

If you’ve updated beyond the patched versions, look for any other usages of Random.InitState or Random.state in the project.

I’m using Unity 2021.3.21f1 and I double checked all scripts there is no State or InitState anywhere.

I once had a project where Random.Range became clogged and so I had to add the line below to the Start() of a script.

Random.InitState((int)System.DateTime.Now.Ticks);

What’s strange is that after running the project I was able to remove the line and Random.Range continued to work as normal.

Just tried that, unfortunately it didn’t help. Still generates the same value.
Edit: I’ll try updating my Unity later, maybe if I get the newer patch this will work.

You could also try an actual instance of System.Random or Unity.Mathematics.Random and not be beholden to static state potentially being mucked around with.

How about passing the PossibleSum as a parameter like…
private int FindMeButton(int PossibleSum){…}

That way you really always know what’s being used in the method. Otherwise it could be simple timing or logic mistakes that make it hard to find the culprit somewhere outside of it.

Another suggestion to better be able to differentiate types of variables. We usually do it like this:
For private class vars using prefix _ as e.g. => “_possibleSum”.
For public vars use capital letter => “PossibleSum”.
And for those only used inside methods without a _ => “possibleSum”.
That makes it easier to see where vars can and should be used in the code.

1 Like

I tried a few things, first of all updating unity doesn’t work.

You could also try an actual instance of System.Random or Unity.Mathematics.Random and not be beholden to static state potentially being mucked around with.

I have tried every single function I could find online with the word “Random” in it, none of them work. I did put these in my code again, still the same result.

How about passing the PossibleSum as a parameter like…
private int FindMeButton(int PossibleSum){…}

I don’t think the parameter is the problem. I tried this, nothing changed. I also tried just

int number = Random.Range(1,5) 

And (if it isn’t out of bounds) it still gives me the same number each time.

Perhaps you’ll get the results you’re expecting if you place the Random.Range line inside your for loop just before line 16.

No, the I need the random number to be generated before the loop as it is compared to the weight of different buttons from the TypeWeights array, which are all set to 1 because the random doesn’t work.
I did put it there, every number is still the same.

Wait you also get the same result every time from an instance of System.Random?

Yes, but only in this fragment of code. Everywhere else it works as intended.