![]() |
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 |
Re: Portable, self-contained replacements for RANDOM_STUFF()
Adobe is NOT standard post script, it's a proprietory format which sucks. To view standard post script documents, get these apps from here
http://www.cs.wisc.edu/~ghost/index.htm I just tested the tiny random algo and it seems to work very well. It's supposed to generate a uniform distribution and is at least as good as rand(). |
Re: Portable, self-contained replacements for RANDOM_STUFF()
well, i think Pagemaker has a PostScript import option, and you might look for other programs that can read PostScript as well.
|
Re: Portable, self-contained replacements for RANDOM_STUFF()
k, I'm getting GhostView and GS 8.11 right now. WTF, they weight LESS than the zipped Acrobat Reader and they do so much more !!!
|
Re: Portable, self-contained replacements for RANDOM_STUFF()
lol - corporate software design for you there ;)
|
Re: Portable, self-contained replacements for RANDOM_STUFF()
btw, I liked this book so much I downloaded the whole thing
http://www.ma.utexas.edu/documentation/nr/bookc/ It is not just about random number generation and has a lot of really usefull programming info. |
Re: Portable, self-contained replacements for RANDOM_STUFF()
yes, I was scared off just when I saw that word: "Knuth" ;)
|
Re: Portable, self-contained replacements for RANDOM_STUFF()
Quote:
Of course! The best comes from Austin! |
Re: Portable, self-contained replacements for RANDOM_STUFF()
This may sound stupid, but should I set the seed more than once to ensure a more random result, or is setting it once enough (as in the numbers are random enough)?
|
Re: Portable, self-contained replacements for RANDOM_STUFF()
You set the seed once before using the rand() function for the first time. Note that the same seed always produces the same line of "random" numbers.
|
Re: Portable, self-contained replacements for RANDOM_STUFF()
Alright thanks, I've been doing it the right way then :)
|
Re: Portable, self-contained replacements for RANDOM_STUFF()
You can use a seed derived from the system clock, that way each time your comupter app runs, it will use a different seed.
|
Re: Portable, self-contained replacements for RANDOM_STUFF()
this is related with this thread, so instead opening a new one, just posting here.
well here is my implemention of random generator derived from an article in "Game Programming Gems", with a bit improvement, which is less "twisted" but random enough I guess: PHP Code:
|
All times are GMT +2. The time now is 23:07. |
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.