Advertisement

Performance of rand()

Started by October 02, 2000 12:56 AM
28 comments, last by Cel 24 years, 3 months ago
Hmm I would have thought with all the advanced or so called advanced AI systems in todays games, random number generation would have been an important factor.

I am surprised at its actual complexity and lack of solid info...

17 cycles for rand() % num;

my god............

Cel
Cel aka Razehttp://chopper2k.qgl.org
Good random numbers are hard to get. You''re trying to get a random number out of a determinant process.

I can find two references for RNG:
- Park and Miller, "Random Number Generators: Good Ones are Hard to Find"
- Forsythe, Malcolm and Moler, "Computer Methods for Mathematical Computations"

Both are referred to by my Matlab reference. Matlab gives a uniform distribution random number of seed = (7^5 * seed) mod (2^31 - 1), and uses this distribution and some other algorithm to derive a normal distribution.

BTW, how many of these 17 cycles are accounted by the overhead of the function call to the library? i.e., if it were inlined, would it be much faster?
Advertisement
i found this one in some tutorial, it's supposed to be from K & R:

int rand_seed=10;

int rand()
{
rand_seed = rand_seed * 1103515245 + 12345;
return (unsigned int)(rand_seed / 65536) % 32768;
}

when i tried this, it was a lot slower than the original rand(), so i tried to optimize it a bit based on what i read here:

int rand_seed=10;

inline int random()
{
rand_seed = rand_seed * 1103515245 + 12345;
return (unsigned int)(rand_seed >> 16) & 32767;
}

notice that this avoids both the slow / and %, it turned out to be al substantial bit quicker than the original, you don't have to write rand() % 128 or something like that either, just change the & 32767 in the return line to the max value you want it to be - 1 (you should not use values greater than 32767)

the rand_seed should have a different value every time you run the program, otherwise you'll end up with the same numbers, just initialize it with the current time or something,

could you people please try this out and let me know if i fucked up somewhere?(i'm not very good at math and my testing program could be all wrong)

remember to test in RELEASE build

Edited by - kris vanhoof on October 3, 2000 2:31:17 PM
hm.. I''ve dont tested it jet.. but, by the way of reading, i thought, that this could boost up your source:
rand_seed = rand_seed * 1103515245 + 12345;

rand_seed*=1103515245;
rand_seed+=12345;

now the question:
is *= or += really faster or isnt it?!

we wanna play, not watch the pictures

If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

For a random number generator, any chaotic iteration will work. If you want a random number which is either 0 or 1 you can RDTSC and then AND eax with 1. This might work with other numbers since the clock cycle count will vary quite a large amount.

------------------------------
#pragma twice


sharewaregames.20m.com

he furby, could you explain that with sourcecode(i dont know much about implemented __asm parts, so i dont know much about eax and this stuff).

that would be really nice

(ps: if you have some more code with __asm - assembler, i wanna know everything)

we wanna play, not watch the pictures

If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

Advertisement
he furby, could you explain that with sourcecode(i dont know much about implemented __asm parts, so i dont know much about eax and this stuff).

that would be really nice

(ps: if you have some more code with __asm - assembler, i wanna know everything)

we wanna play, not watch the pictures

i''m sorry for the double post with an ap.. i just have thought it hasnt posted, because i havent seen the 1/2 at the bottom..


sorry

we wanna play, not watch the pictures

If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

The clock is not chaotic; it''s cyclic. Even if the source frequency and the sampling frequency aren''t coupled, aliasing can occur which will make your "random" variable a chaotic variable.
rand() % n is a bad way to get random numbers in a certain range, because many, many implementations of rand are distressingly non-random in the lower bits. See the C faq for info and a somewhat better solution: http://www.eskimo.com/~scs/C-faq/q13.16.html

As for intel''s noise generator: The only way to get at it is through higher level APIs like Microsoft''s Crypto API. It''s a lot more effort than it''s worth for a game, if you ask me.

This topic is closed to new replies.

Advertisement