Performance of rand()
Heya, I am wanting to make a lot of my AI behaviour contain random movements/decisions etc.
The only way I know of to create a random number is by using
rand() % maxRange;
Now I am wondering how efficient is this function and is there a fast(er?) way to generate random numbers?
the rand() function will probably get called quite a few times per cycle so I want to tweak it if possible.
Cel
Cel aka Razehttp://chopper2k.qgl.org
i''ve found that rand isn''t to bad (i called it 45 times in a for loop and didn''t drop a single frame) but stay away from calling srand, or rather only call it once, i found this slowed things down a ton.
-------------------------------------------------
Don't take life too seriously, you''ll never get out of it alive. -Bugs Bunny
-------------------------------------------------
Don't take life too seriously, you''ll never get out of it alive. -Bugs Bunny
-------------------------------------------------Don't take life too seriously, you''ll never get out of it alive. -Bugs Bunny
October 02, 2000 01:19 AM
rand() is just implemented as a bunch of shifts, multiplies, and adds. so it''s nothing whacky. certainly it''s O(1).
Sweet ta, if you are running it 45 times a cycle with no performance hit, thats plenty for me
Cel
Cel
Cel aka Razehttp://chopper2k.qgl.org
of course i should add that i''m on a pIII 700, with a geForce2, and that might have something to do with it... but anyway like i said before the srand killed the frame rate, but the i relized i should only seed the genorator once... anyway i''m off
-------------------------------------------------
Don't take life too seriously, you''ll never get out of it alive. -Bugs Bunny
-------------------------------------------------
Don't take life too seriously, you''ll never get out of it alive. -Bugs Bunny
-------------------------------------------------Don't take life too seriously, you''ll never get out of it alive. -Bugs Bunny
just remember % uses a divide which takes 17 cycles.
rather than using %, use the equivilant:
rand() & (maxvalue - 1)
see - 10 % 3 is the same as 10 & 2
and a bitwise AND is MUCH faster
rather than using %, use the equivilant:
rand() & (maxvalue - 1)
see - 10 % 3 is the same as 10 & 2
and a bitwise AND is MUCH faster
nice to know, quantum
do you know how many cicles a multiplication has, too?!
but first of all big thanks for the rand()-optimisation
we wanna play, not watch the pictures
do you know how many cicles a multiplication has, too?!
but first of all big thanks for the rand()-optimisation
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 Quantum
just remember % uses a divide which takes 17 cycles.
rather than using %, use the equivilant:
rand() & (maxvalue - 1)
see - 10 % 3 is the same as 10 & 2
and a bitwise AND is MUCH faster
Sound nice, but, for example, "rand() & 128" will return either 0 or 128, not any number in between!
-Jussi
int rand1,rand2;
int i;
int dave=0;
for(int y=0;y<256*256;y++)
{
i=rand();
if(y)
{
rand1=(i%y);
rand2=(i&(y-1));
if(rand1!=rand2)
dave++;
}
}
that was a little test...
then print dave: about 64000!!
Quantum, I dont think, your optimization work fine
int i;
int dave=0;
for(int y=0;y<256*256;y++)
{
i=rand();
if(y)
{
rand1=(i%y);
rand2=(i&(y-1));
if(rand1!=rand2)
dave++;
}
}
that was a little test...
then print dave: about 64000!!
Quantum, I dont think, your optimization work fine
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
Selkrank, and Cel:
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).
/Sayonara
"garbage in garbagecan? hm.. makes sense"
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).
/Sayonara
"garbage in garbagecan? hm.. makes sense"
"garbage in garbagecan? hm.. makes sense"
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement