Forum

Random Functions that People Might Find Useful!!!

Discuss programming in the QuakeC language.

Moderator: InsideQC Admins

Random Functions that People Might Find Useful!!!

Postby Error » Wed Oct 06, 2010 8:53 am

These are just a few that I've found quite useful in my mods. Feel free to post your own, as they may make peoples' lives easier.

I know these aren't the best, maybe could be simplified, but they'd be good for a newb to have. Maybe......

I'll feel like an idiot if I typed those in wrong :lol:

Code: Select all
//
// Random Whole Number Generator
// Use: to find a whole number between lower and higher
//
float(float lower, float higher) rand =
{
   local float ra;
   ra = lower + (random() * (higher - lower);
   ra = rint(ra); // rounds the number
   return ra;
};

//
// Convert Feet to Quake Units
// Use: Easier for the programmer to understand
//
float(float feet) ft_to_qu =
{
   local float qu;
   qu = feet * 8;
   return qu;
};

//
// Convert Quake Units to Feet
// Use: Easier to display information to the player
//
float(float qu) qu_to_ft =
{
   local float ft;
   ft = qu / 8;
   return ft;
};
User avatar
Error
InsideQC Staff
 
Posts: 865
Joined: Fri Nov 05, 2004 5:15 am
Location: VA, USA

Postby Chip » Wed Oct 06, 2010 9:13 am

Thanks, this could be the beginning of a snippet collection.
QuakeWiki
getButterfly - WordPress Support Services
Roo Holidays

Fear not the dark, but what the dark hides.
User avatar
Chip
 
Posts: 575
Joined: Wed Jan 21, 2009 9:12 am
Location: Dublin, Ireland

Postby andrewj » Wed Oct 06, 2010 9:58 am

Error, your rand() function should use floor() instead of rint(), otherwise the results are not uniform.

Example of results of your code generating numbers 1 to 8:

1: 64341
2: 129276
3: 128774
4: 129092
5: 129327
6: 128659
7: 129035
8: 64320
andrewj
 
Posts: 133
Joined: Mon Aug 30, 2010 3:29 pm
Location: Australia

Postby Tomaz » Wed Oct 06, 2010 1:35 pm

QU to Metre would make more sense ;)

Also I agree about the floor with a small addition, need to add 0.999 to the higher

1-8 makes it rand between 1.000 t0 8.999 with a floor making it pretty much equal chance to get any of the numbers 1-8
Tomaz
 
Posts: 67
Joined: Fri Nov 05, 2004 8:21 pm

Postby frag.machine » Wed Oct 06, 2010 2:51 pm

Whn programming in QuakeC, I only force random number to ints if unavoidable. Consider this example:
Code: Select all
local float r;
r = 1 + rint (random() * 8);
// or, as suggested
// r = 0.9999 + floor (random() * 8);
if (r == 1)
{
 // do something
}
else if (r == 2)
{
// do something else
}
(...)


Most of the cases this can be safely replaced by:
Code: Select all
local float r;
r = random ();
if (r > 0.87) // 0.87 == (1/8)*7 more or less :)
{
// do something
}
else if (r > 0.75)
{
// do something else
}
(...)


If we are talking about code being called inside an intensive loop (for example, iterating thru a long entity chain list generated by a call to findradius() or find()), avoiding the extra builtin call and sum can bring some gain in performance terms.
I know FrikaC made a cgi-bin version of the quakec interpreter once and wrote part of his website in QuakeC :) (LordHavoc)
User avatar
frag.machine
 
Posts: 2090
Joined: Sat Nov 25, 2006 1:49 pm

Postby Spike » Wed Oct 06, 2010 4:09 pm

fteqcc actually has some intrinsics.
random() -> returns 0-1
random(x) -> returns 0-x
random(x,y) -> returns x-y
randomv() -> returns '0 0 0' to '1 1 1'
randomv(x) -> returns '0 0 0' to x
randomv(x,y) -> follow the trend, they're vectors, its a cube.

fteqcc supports these, as they are required for hexenc code too (fteqcc directly supports both syntaxes thus supports these, even for qc code).
Results are not rounded.

Thus mixing that with switch statements:
switch(random(3))
{
case 0 .. 1:
dostuff1();
break;
case 1 .. 2:
dostuff2();
break;
case 2 .. 3:
dostuff3();
break;
}

(its a double-dot between the two bounds)

Also, I feel strangely compelled to point out that if your player is 6'6" tall, then hes also 4' wide. Seriously, that guy needs to go on a diet. :P
Spike
 
Posts: 2892
Joined: Fri Nov 05, 2004 3:12 am
Location: UK

Postby Error » Wed Oct 06, 2010 8:15 pm

thanks andrewj
User avatar
Error
InsideQC Staff
 
Posts: 865
Joined: Fri Nov 05, 2004 5:15 am
Location: VA, USA

Postby mrmmaclean » Wed Oct 06, 2010 10:24 pm

Spike, thanks for the info on fteqcc. I've been using it for compiling but haven't really looked into documentation that much (oops) and those features totally slipped under my radar!
User avatar
mrmmaclean
 
Posts: 33
Joined: Sun Aug 22, 2010 2:49 am


Return to QuakeC Programming

Who is online

Users browsing this forum: No registered users and 1 guest