- Home /
Changing the range of Random.Range during runtime - C#
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
It's unclear whether those are intentional if's, nested if's or intended (but forgotten) else-if's. Therefore its difficult for anyone to assist further. As Habitablaba says, sort the formatting out please.
They aren't else -ifs because only one will be true at any given time. The code just runs down checking to see if any of them meet the requirements. If they do the code it executes will prevent it from ever running again. Range changes perfectly but the Random.Range still returns values in the same range
Ok. So it IS intended that on some occasions, every single line above will be executed one after the other
Answer by Bunny83 · Oct 27, 2014 at 12:17 PM
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
}
}
Yes I have the randomNumber as public and it still outputs numbers greater over 400. I know there are probably other methods to do this but I was wondering why the Ranom.Range doesn't acknowledge the change in the maximum value.
That's actually not possible. Try adding a Debug.Log like this to your original code:
randomNumber = Random.Range(0, randomRange);
Debug.Log("Random number: " + randomNumber + " out of 0 - " + randomRange);
If randomRange is 400 you won't get values equal or greater than 400. Are you sure you use the correct variables or maybe you have another local variable named randomNumber or randomRange.
I'm going to have to apologize. I feel like laughing at myself. The code was working properly but I had my Random.Range generator under 2 if-statements and I had only changed the number generator for the one that was not being executed. I'm going to mark your question as having answered $$anonymous$$e for your help. Thanks. Was quite a funny experience when I was copying my source code into a word processor and I noticed that the former if-statement wasn't changed to include randomRange.
Your answer