SUB_RandomRange ()

Discuss programming in the QuakeC language.
Post Reply
Cobalt
Posts: 445
Joined: Wed Jun 10, 2009 2:58 am
Location: New England, USA
Contact:

SUB_RandomRange ()

Post by Cobalt »

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);
};






Spike
Posts: 2914
Joined: Fri Nov 05, 2004 3:12 am
Location: UK
Contact:

Re: SUB_RandomRange ()

Post by Spike »

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.
Cobalt
Posts: 445
Joined: Wed Jun 10, 2009 2:58 am
Location: New England, USA
Contact:

Re: SUB_RandomRange ()

Post by Cobalt »

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?
jitspoe
Posts: 217
Joined: Mon Jan 17, 2005 5:27 am

Re: SUB_RandomRange ()

Post by jitspoe »

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.
Spike
Posts: 2914
Joined: Fri Nov 05, 2004 3:12 am
Location: UK
Contact:

Re: SUB_RandomRange ()

Post by Spike »

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.
Post Reply