SUB_RandomRange ()
Moderator: InsideQC Admins
5 posts
• Page 1 of 1
SUB_RandomRange ()
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".
- Code: Select all
/*
============
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);
};
-

Cobalt - Posts: 445
- Joined: Wed Jun 10, 2009 2:58 am
- Location: New England, USA
Re: SUB_RandomRange ()
using fteqcc?
random() returns 0-1
random(5) returns 0-5
random(5,10) returns 5-10
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.
random() returns 0-1
random(5) returns 0-5
random(5,10) returns 5-10
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.
- Spike
- Posts: 2892
- Joined: Fri Nov 05, 2004 3:12 am
- Location: UK
Re: SUB_RandomRange ()
Didnt know about those.
FrikQcc compiles those with a "complex type" warning, however the engine crashes when its ran. FTE does them just fine.
In my tests with random (5,10), the printout never shows a 10.0 - only 9.9xxxxx which you had mentioned.
If we did:
(rint(random(5,10)
I suppose then we would see a 10 result at some point?
FrikQcc compiles those with a "complex type" warning, however the engine crashes when its ran. FTE does them just fine.
In my tests with random (5,10), the printout never shows a 10.0 - only 9.9xxxxx which you had mentioned.
If we did:
(rint(random(5,10)
I suppose then we would see a 10 result at some point?
-

Cobalt - Posts: 445
- Joined: Wed Jun 10, 2009 2:58 am
- Location: New England, USA
Re: SUB_RandomRange ()
Spike wrote:using fteqcc?
random() returns 0-1
random(5) returns 0-5
random(5,10) returns 5-10
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.
- jitspoe
- Posts: 217
- Joined: Mon Jan 17, 2005 5:27 am
Re: SUB_RandomRange ()
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.
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.
- Spike
- Posts: 2892
- Joined: Fri Nov 05, 2004 3:12 am
- Location: UK
5 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 1 guest