Random Functions that People Might Find Useful!!!
Moderator: InsideQC Admins
8 posts
• Page 1 of 1
Random Functions that People Might Find Useful!!!
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
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
- 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;
};
-

Error - InsideQC Staff
- Posts: 865
- Joined: Fri Nov 05, 2004 5:15 am
- Location: VA, USA
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.
getButterfly - WordPress Support Services
Roo Holidays
Fear not the dark, but what the dark hides.
-

Chip - Posts: 575
- Joined: Wed Jan 21, 2009 9:12 am
- Location: Dublin, Ireland
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
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
Whn programming in QuakeC, I only force random number to ints if unavoidable. Consider this example:
Most of the cases this can be safely replaced by:
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.
- 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)
-

frag.machine - Posts: 2090
- Joined: Sat Nov 25, 2006 1:49 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
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
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!
-

mrmmaclean - Posts: 33
- Joined: Sun Aug 22, 2010 2:49 am
8 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 1 guest