![]() |
Portable, self-contained replacements for RANDOM_STUFF()
since we can't use rand() for replacing RANDOM_LONG() and RANDOM_FLOAT() and thus save costy engine calls each time, because rand() only returns short integers and not longs, a portable, fast, and self-contained random number generator is something nice indeed
irwigo, w00t w00t mateys Code:
// RACC - AI development project for first-person shooter games derived from Valve's Half-Life |
Re: Portable, self-contained replacements for RANDOM_STUFF()
aha, one feature is missing though.
To make them behave exactly like the engine's RANDOM_LONG() and RANDOM_FLOAT(), add this check on top of the RandomLong() and RandomFloat() functions: PHP Code:
|
Re: Portable, self-contained replacements for RANDOM_STUFF()
I have somewhere a random number lib, but I guess it's not really useful for our purposes here ... it's just providing functions for random numbers based on the first 10 million or whatever you want digits of pi :D
pi is known to be a really good random number source ... |
Re: Portable, self-contained replacements for RANDOM_STUFF()
This lrand() seems not very random to me, I think rand() uses more 'randomly' algorithm. But maybe it's just me ... :D
|
Re: Portable, self-contained replacements for RANDOM_STUFF()
Quote:
lrand_lo = rand() lrand_hi = rand() lrand_lo += (lrand_hi & 0x7FFF) << 16; // obviously more thought behind this idea will be // needed to make this work properly Also don't forget that mostly scientific applications require highly precise random number generation. For gamming purposes even using bits from the system clock is good enough, and what you normally favor is performance over accuracy. For the most part 16 bit rand() is more than good enough I think. Can anyone come up with a reason why 16 bots is not enough? |
Re: Portable, self-contained replacements for RANDOM_STUFF()
yes... more than 32767 waypoints/nodes/walkfaces/navlinks/whatever.
One reason among others :) Also, I assure you that the random results are very convincing : the algorithm is based on an infinite math suite, whose start point is determined by the initial seed - and this implementation is VERY fast, much faster than calling rand() twice. I believe the guy who wrote this did a math paper somewhere, it must be lying around on the net ;) |
Re: Portable, self-contained replacements for RANDOM_STUFF()
***BUGBUGBUG***
The RandomFloat() function was incorrect! It was returning numbers above the limit due to the "(to - from + 1)" bit! It's fixed now. When I say I can't line up two words in C without writing a state of the art bug! :( |
Re: Portable, self-contained replacements for RANDOM_STUFF()
Quote:
S.K. Park and K.W. Miller, Communications of the ACM 31:10 (Oct 1988, and "Two Fast Implementations of the 'Minimal Standard' Random Number Generator", David G. Carta, Comm. ACM 33, 1 (Jan 1990), p. 87-88 linear congruential generator f(z) = 16807 z mod (2 ** 31 - 1) uses L. Schrage's method to avoid overflow roblems I found this on-line postscript book http://www.ma.utexas.edu/documentation/nr/bookc/ Look at c7-1.ps it discusses a lot of things, and it says this is probably the simplest and fastest random number generator of all Code:
unsigned long idnum; |
Re: Portable, self-contained replacements for RANDOM_STUFF()
what do you use for viewing .ps files under Windows ? Acrobat Reader 6 doesn't seem to want to read it :(
I can't wait to see what they say, because your tiny number generator looks really lovely ! |
Re: Portable, self-contained replacements for RANDOM_STUFF()
Quote:
And your have a mis-leading comment in your code. float RandomFloat (float from, float to) { // this function returns a random integer |
All times are GMT +2. The time now is 14:54. |
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.