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;
}
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.
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.
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.
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.