Advertisement

Performance of rand()

Started by October 02, 2000 12:56 AM
28 comments, last by Cel 24 years, 3 months ago
quote: Original post by qbvet

Yup, that''s why we subtract one, from 128 in this case.
128-1 = 127 (01111111) which is the bitmask used for
anding. But if you mean something like this: x % 129, we
have a problem due to the fact that 129 isn''t a power of two.
So when trying to use x & (129-1) instead of x % 129 we''ll
get a bad result because the mask, 128 (10000000) will just
deliver 128, or 0 -just like you said, Selkrank- when using the bitwise and-operator on x, and the mask. So if you are going to
use x & (y - 1) instead of x % y, you have to make sure
that y is a power of 2.. x & (2^z - 1).


Oh, of course. So, it''s fine when your range is a power of two. Hmm...

-Jussi
Yup :-)
Which is good, when computer-numbers often
are powers of two. /Sayonara

"garbage in garbagecan? hm.. makes sense"
"garbage in garbagecan? hm.. makes sense"
Advertisement
Hi there.

Intel has been talking about hardware random number generators (RNG) on their new motherboards and processors. Check this site out:

ftp://download.intel.com/design/chipsets/rng/techbrief.pdf

-René
I''m not sure standard rand is highly optimized.
I doubt there are strong rules regarding rand implementation in a compiler, so performance could vary.

When I was tuning my previous particle system, I tried to use a custom random number generator (that was supposed to generate numbers the same way rand() does) and noticed my customized version was faster.

If you''re calling it 45 times per frame, go with rand(), but if you need to use a huge particle system, or a lot of small ones you could get something like 1500-2000 rands per frame and in this case go for a customized version.
Especially with particle systems, if you want to add 3-axis random movement, you need to calc rand 3 times. Furthermore, if you want to randomize parameters like speed, lifetime, etc., rand can significantly slow down your code. You can tweak, of course, but to tweak something already tweaked could bring to tricky results.
How bout in your installation, you create a file with 10000000 random numbers. Then you just read them in.
or you could create a pointer:

char* rand=(char*)malloc(1);
free(rand);
int* x=(int*)rand;
rand=(char)*x;

or something like this should work..

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
main()
{
char* rand=(char*)malloc(1);
free(rand);
int x=(int)rand;
printf("%i\n",rand);
printf("%i\n",x);
return 0;
}

..works fine:

CONSOLE-OUT:
7737056
7737056
Press Enter To Continue...

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

ok--- doesnt work as fine as i thougt.. it returns everytime the same value (??!??!?!?)

anyone?!

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

quote: Original post by davepermen

ok--- doesnt work as fine as i thougt.. it returns everytime the same value (??!??!?!?)

anyone?!

we wanna play, not watch the pictures



That''s probably because nothing else has been written to that block of memory yet. Try rebooting your computer, that should give you a different number .

==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
I don't think getting the value of a discarded memory chunk constitutes a good RNG :-)

For performance-cirtical apps (that again don't require really random numbers) use something like this:

        #define MAX_RAND 1000static int g_rgnRnadom[ MAX_RAND ];static int g_nIndex = 0;//////void InitRandom( int MaxRange ){  for( int ix=0; ix<MAX_RAND; ix++ )  {    g_rgnRandom[ix] = rand()%MaxRange;  }}//////inline void SeedRandom( int seed ){   if ( seed < 0 ) { seed = -seed; }   g_nIndex = seed % MAX_RAND;}//////inline int Random(){   g_nIndex++;   if ( g_nIndex >= MAX_RAND ) { g_nIndex=0; }   return g_rgnRandom[ g_nIndex ];}    


...of course to get rid of the if-statement in Random() you could make g_nIndex an unsigned short (assuming, of course, RAND_MAX is (unsigned short)0xffff).

Hope this gives you a few ideas.







Edited by - neocron on October 2, 2000 6:46:30 PM

This topic is closed to new replies.

Advertisement