Found a neat piece of code for grabbing a float in a specified range. Experienced QC coders probably can probably get the same result by typing out exactly what they want to do using random () , but this code can come in handy. I found it in the old QC mod called : "extras".
/*
============
SUB_RandomRange
Return a random number between min & max (or min if max is 0)
Just make sure max is greater than min...
============
*/
float(float rmin, float rmax) SUB_RandomRange =
{
if (!rmax)
return rmin;
else
return rmin + random()*(rmax-rmin);
};
its a hexenc feature that fteqcc emulates with a formula much like you specified.
unlike that function, fteqcc has no special check for the min/max value, which means it works with any range you specify, even inverted ranges, so you might want to validate that still.
note that some engines will never return 1 from basic random, while vanilla-like engines can. This change is useful when the random value is then floored and used as an array index. the vanilla-like engines may end up trying to read out of bounds, while an engine that never returns 1 will be fine in this case.
its a hexenc feature that fteqcc emulates with a formula much like you specified.
unlike that function, fteqcc has no special check for the min/max value, which means it works with any range you specify, even inverted ranges, so you might want to validate that still.
note that some engines will never return 1 from basic random, while vanilla-like engines can. This change is useful when the random value is then floored and used as an array index. the vanilla-like engines may end up trying to read out of bounds, while an engine that never returns 1 will be fine in this case.
Heh, random returning 1 is always fun when you get that one in RND_MAX crash... I hit that one with random footstep sounds once.
rint will round towards the nearest value. rint(random(5,10)) will thus give you about the following chances of getting the specified number:
10% 5
20% 6
20% 7
20% 8
20% 9
10% 10
so basically, you're probably better off using floor(random(5,11)) if you want an even distribution between 5 and 10... and trying again if you get 11 (or use ranges such that 11 ends up lumped in with 10 anyway).
ceil(random(5,10)) is just awkward. you'll always have a chance of getting 5, but it'll be a really slim chance, so its probably not desirable.