I want to change the range of a random number generator as time passes making it more likely that the generator chooses a certain number making my game less based on luck. For some reason my generator doesn’t seem to acknowledge the change in the max value and continues to return values according to the previous range. Kind of hard to format in this text box but these are single line if statements and therefore I didn’t use brackets. These are under the void Update() method. The if statements systematically change the upper range of the random class so there is a greater probability of the number “50” being chosen. Since I have randomNumber as public I can see I in the inspector and it seems to still adhere to the old range. Also I can see randomRange changing; it just doesn’t seem to make a difference to the Random.Range method. Code:
if ((Time.time >= 10) && (randomRange == 500))
randomRange = 400;
if ((Time.time >= 120) && (randomRange ==400))
randomRange = 300;
if ((Time.time >= 240) && (randomRange == 300))
randomRange = 100;
randomNumber = Random.Range(0, randomRange);
if (randomNumber == 50)
//the code I want to execute.. Not relevant to question
Well, if you see that randomRange changes, then your Random.Range call will use the new range. You said it seems that it doesn’t use the new range, what have you done to verify that? Is randomNumber a public variable? If not, make it public so you can see it in the inspector. Once randomRange is 400 there shouldn’t be any number greater or equal to 400.
Do you just have the “feeling” it doesn’t change anything? That’s probably because there’s almost no difference between 1 in 500 or 1 in 400 or 1 in 300. Even 1 in 100 can take ages to get one specific value. It’s random, that’s the point, isn’t it? You increase the possibility slightly but that doesn’t mean that you see an instant effect. Random events like this can’t be predicted in any way. The probability is only the average across infinite uses.
If you just want to have different spawn delays which vary randomly you should pick one random number and use it as delay value. For example:
float nextEvent = 0;
float randomRange = 60f; // start with 30 - 60 seconds delay
void Update()
{
if (Time.time > nextEvent)
{
if (Time.time > 240)
randomRange = 10f; // 10 - 20 seconds delay after 4 minutes
else if (Time.time > 120)
randomRange = 30f; // 15 - 30 seconds delay after 2 minutes
else if (Time.time > 10)
randomRange = 40f; // 20 - 40 seconds delay after 10 seconds
nextEvent = Time.time + Random.Range(randomRange/2, randomRange);
// Do your "not relevant" stuff here
}
}