How can I get a random float within a range, excluding a sub-range? The only idea I have so far is to create two ranges and randomly choose from those two. I have to use float Random.Range(float min, float max)
method found in unity3d engine, so the code would be:
float GetRandomNumber(float min, float max, float minExclusive, float maxExclusive)
{
float outcome;
if (Random.Range(0, 1) < 0.5f)
outcome = Random.Range(min, minExclusive);
else
outcome = Random.Range(maxExclusive, max);
return outcome;
}
I can safely assume that min < minExclusive < maxExclusive < max
.
I tried googling around but the only similar problems I've found concern integers and they behave a bit differently (e.g. you can easily iterate over them):
- Generating random number excluding range
- How can I generate a random number within a range but exclude some?
Does anyone have a better idea?