A better rand()?

I’m doing some work with fractals and, well, I’m spending a whole lot of time
in rand() and I was wondering if there was a better alternative?

Here’s the break down of the profiling
% time seconds usecs/call calls function


83.68 79.397830 39 2000090 rand
16.19 15.365268 38 400008 SDL_MapRGB
0.05 0.047411 5926 8 SDL_Flip
0.04 0.039446 39446 1 SDL_SetVideoMode
0.02 0.016070 64 251 SDL_PollEvent
0.01 0.013374 13374 1 SDL_Quit
0.01 0.005306 5306 1 SDL_Init
0.00 0.000117 14 8 SDL_UnlockSurface
0.00 0.000109 13 8 SDL_LockSurface
0.00 0.000027 27 1 srand
0.00 0.000023 23 1 time


100.00 94.884981 2400378 total–
"They that can give up essential liberty to obtain a little temporary safety
deserve neither liberty nor safety. "
–Benjamin Franklin

Oops, never mind.On Wednesday 12 March 2003 5:21 pm, Samuel wrote:

I’m doing some work with fractals and, well, I’m spending a whole lot of
time in rand() and I was wondering if there was a better alternative?

Here’s the break down of the profiling
% time seconds usecs/call calls function


83.68 79.397830 39 2000090 rand
16.19 15.365268 38 400008 SDL_MapRGB
0.05 0.047411 5926 8 SDL_Flip
0.04 0.039446 39446 1 SDL_SetVideoMode
0.02 0.016070 64 251 SDL_PollEvent
0.01 0.013374 13374 1 SDL_Quit
0.01 0.005306 5306 1 SDL_Init
0.00 0.000117 14 8 SDL_UnlockSurface
0.00 0.000109 13 8 SDL_LockSurface
0.00 0.000027 27 1 srand
0.00 0.000023 23 1 time


100.00 94.884981 2400378 total


"They that can give up essential liberty to obtain a little temporary safety
deserve neither liberty nor safety. "
–Benjamin Franklin

That’s going to depend heavily on how the current rand() you’re using
is implemented. The general first response to this type of thing
though is “look up Mersenne Twister”. I know it’s a far better pRNG
than the typical linear-congruential rand(), but performance wise I
don’t know how it compares.

BrianOn Wed, 12 Mar 2003 17:21:10 -0800, Samuel wrote:

I’m doing some work with fractals and, well, I’m spending a whole
lot of time in rand() and I was wondering if there was a better
alternative?

Here’s the break down of the profiling % time seconds
usecs/call calls function



83.68 79.397830 39 2000090 rand 16.19 15.365268
38 400008 SDL_MapRGB 0.05 0.047411 5926
8 SDL_Flip 0.04 0.039446 39446 1
SDL_SetVideoMode 0.02 0.016070 64 251
SDL_PollEvent 0.01 0.013374 13374 1 SDL_Quit
0.01 0.005306 5306 1 SDL_Init 0.00
0.000117 14 8 SDL_UnlockSurface 0.00
0.000109 13 8 SDL_LockSurface 0.00
0.000027 27 1 srand 0.00 0.000023
23 1 time


100.00 94.884981 2400378 total

Hey! great, thanks for the code snipit.
you are right about the system rand
even after i fixed my “stoner moment” in the code it still was more sluggis
than I cared for. and rand is still eating up my cpu cycles, i’ll give this a
try.

Thanks again.
SamuelOn Wednesday 12 March 2003 5:45 pm, you wrote:

Oops. I sent this to the list from the wrong address, and it’s sitting
waiting for approval; I don’t want to resend it since it’s bound to
end up getting posted twice, but it’s likely to take a few days so I’ll
just forward the post to you.


"They that can give up essential liberty to obtain a little temporary safety
deserve neither liberty nor safety. "
–Benjamin Franklin

Hey! great, thanks for the code snipit.
you are right about the system rand
even after i fixed my “stoner moment” in the code it still was more
sluggis than I cared for. and rand is still eating up my cpu cycles,
i’ll give this a try.

Thanks again.
Samuel

Oops. I sent this to the list from the wrong address, and it’s
sitting waiting for approval; I don’t want to resend it since it’s
bound to end up getting posted twice, but it’s likely to take a few
days so I’ll just forward the post to you.

Might wanna try reading from /dev/urandom as well. It will give better
randomness instead of the standard C thingy.

Or if your mobo have random number generator like mine use it.On Thursday 13 March 2003 03:54, Samuel wrote:

On Wednesday 12 March 2003 5:45 pm, you wrote:

U?ytkownik Sami N??t?nen <sami.naatanen at kolumbus.fi> napisa?:

Hey! great, thanks for the code snipit.
you are right about the system rand
even after i fixed my “stoner moment” in the code it still was more
sluggis than I cared for. and rand is still eating up my cpu cycles,
i’ll give this a try.

Thanks again.
Samuel

Oops. I sent this to the list from the wrong address, and it’s
sitting waiting for approval; I don’t want to resend it since it’s
bound to end up getting posted twice, but it’s likely to take a few
days so I’ll just forward the post to you.

Might wanna try reading from /dev/urandom as well. It will give better
randomness instead of the standard C thingy.

Or if your mobo have random number generator like mine use it.

if your problem is mainly speed you can try to set random numbers array
( or file with random nubers or any other storage ) at game start
( if this array is large enough ) nobody will notice the difference
PAMASH>On Thursday 13 March 2003 03:54, Samuel wrote:

On Wednesday 12 March 2003 5:45 pm, you wrote:


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Or if your mobo have random number generator like mine use it.

Just curious, and excuse my ignorance, as I’m rather new to SDL…
…does this make for unportable code, or do most MB’s have a RNG that SDL
can call portably?

----- Original Message -----

Sam, since truly random numbers are impossible to generate, what your
looking for is a sequence of numbers which apear random.

I suggest you look up “chaotic functions” on google. Chaotic functions are
functions which are sensitive to intial conditions. That means if you put 3
into the function it could give you 43, but if you put 4, it could give you
1,536,938,657 and if you put 5, it could give you 200.

Hows this help you get random numbers you ask? Well at the begining of your
program you:

srand((unsigned)time(NULL)); //seeds random number generator with current
miliseconds

int Start=rand(); //Start=0-65535

what you do is plug Start into your chaotic function to get the first random
number (lets say its 5). Then what you do is save this 5 so next time you
want a random number, you plug the 5 into the chaotic equation to get the
next random number, and keep going in this cycle.

Heres some code of how such a function would work:

#include <time.h>
#include <rand.h> //i think this is it…might be stdlib or math…i forget

int ChaosRand(void)
{
static int LastNum=0;

if(LastNum==0)
{
srand((unsigned)time(NULL))
LastNum=rand();
}

//use LastNum as the input number into your chaotic function
//calc. the next number using the chaotic function
LastNum=somechaoticfunc(LastNum);

return lastNum;

}

thats the general idea. Now all you have to do is find a chaotic function
which makes you reasonably happy in that it runs fast enough and gives you
substantialy random numbers. You use this function the same way youd use
rand() so if you wanted a random number between 0 and 9 it would be:

int i = ChaosRand()%10;

of course if you want something just quick and done, you could pre-generate
5000 random numbers when your program fires up keeping an index of where you
are in the array and when you run out, regenerate 5000 random numbers and
set the index to 0. That would also make things nice and fast.

Hope this helps!
-Atrix

PS if you dig this and want to learn more about chaos, fractals and lots of
other cool stuff, i HIGHLY recomend a book called “the computational beauty
of nature”, i got mine off amazon.> ----- Original Message -----

From: pamashoid@poczta.onet.pl ()
To:
Sent: Thursday, March 13, 2003 3:45 AM
Subject: [SDL] A better rand()?

U?ytkownik Sami N??t?nen <sami.naatanen at kolumbus.fi> napisa?:

On Thursday 13 March 2003 03:54, Samuel wrote:

Hey! great, thanks for the code snipit.
you are right about the system rand
even after i fixed my “stoner moment” in the code it still was more
sluggis than I cared for. and rand is still eating up my cpu cycles,
i’ll give this a try.

Thanks again.
Samuel

On Wednesday 12 March 2003 5:45 pm, you wrote:

Oops. I sent this to the list from the wrong address, and it’s
sitting waiting for approval; I don’t want to resend it since it’s
bound to end up getting posted twice, but it’s likely to take a few
days so I’ll just forward the post to you.

Might wanna try reading from /dev/urandom as well. It will give better
randomness instead of the standard C thingy.

Or if your mobo have random number generator like mine use it.

if your problem is mainly speed you can try to set random numbers array
( or file with random nubers or any other storage ) at game start
( if this array is large enough ) nobody will notice the difference
PAMASH


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

The best pseudo random number generator free for use in internet is
ISAAC is so good what you can use to statistical issues. You can find
this in

http://burtleburtle.net/bob/rand/isaacafa.html

Best regards,

Frank Alcantara.
Thyamad Consultoria.
@Frank_Alcantara
http://www.thyamad.com

-----Mensagem original-----De: sdl-admin at libsdl.org [mailto:sdl-admin at libsdl.org] Em nome de Atrix
Wolfe
Enviada em: quinta-feira, 13 de mar?o de 2003 14:03
Para: sdl at libsdl.org
Assunto: Re: [SDL] A better rand()?

Sam, since truly random numbers are impossible to generate, what your
looking for is a sequence of numbers which apear random.

I suggest you look up “chaotic functions” on google. Chaotic functions
are
functions which are sensitive to intial conditions. That means if you
put 3
into the function it could give you 43, but if you put 4, it could give
you
1,536,938,657 and if you put 5, it could give you 200.

Hows this help you get random numbers you ask? Well at the begining of
your
program you:

srand((unsigned)time(NULL)); //seeds random number generator with
current
miliseconds

int Start=rand(); //Start=0-65535

what you do is plug Start into your chaotic function to get the first
random
number (lets say its 5). Then what you do is save this 5 so next time
you
want a random number, you plug the 5 into the chaotic equation to get
the
next random number, and keep going in this cycle.

Heres some code of how such a function would work:

#include <time.h>
#include <rand.h> //i think this is it…might be stdlib or math…i
forget

int ChaosRand(void)
{
static int LastNum=0;

if(LastNum==0)
{
srand((unsigned)time(NULL))
LastNum=rand();
}

//use LastNum as the input number into your chaotic function
//calc. the next number using the chaotic function
LastNum=somechaoticfunc(LastNum);

return lastNum;

}

thats the general idea. Now all you have to do is find a chaotic
function
which makes you reasonably happy in that it runs fast enough and gives
you
substantialy random numbers. You use this function the same way youd
use
rand() so if you wanted a random number between 0 and 9 it would be:

int i = ChaosRand()%10;

of course if you want something just quick and done, you could
pre-generate
5000 random numbers when your program fires up keeping an index of where
you
are in the array and when you run out, regenerate 5000 random numbers
and
set the index to 0. That would also make things nice and fast.

Hope this helps!
-Atrix

PS if you dig this and want to learn more about chaos, fractals and lots
of
other cool stuff, i HIGHLY recomend a book called “the computational
beauty
of nature”, i got mine off amazon.

----- Original Message -----
From: pamashoid@poczta.onet.pl ()
To:
Sent: Thursday, March 13, 2003 3:45 AM
Subject: [SDL] A better rand()?

U?ytkownik Sami N??t?nen <sami.naatanen at kolumbus.fi> napisa?:

On Thursday 13 March 2003 03:54, Samuel wrote:

Hey! great, thanks for the code snipit.
you are right about the system rand
even after i fixed my “stoner moment” in the code it still was more
sluggis than I cared for. and rand is still eating up my cpu
cycles,

i’ll give this a try.

Thanks again.
Samuel

On Wednesday 12 March 2003 5:45 pm, you wrote:

Oops. I sent this to the list from the wrong address, and it’s
sitting waiting for approval; I don’t want to resend it since
it’s

bound to end up getting posted twice, but it’s likely to take a
few

days so I’ll just forward the post to you.

Might wanna try reading from /dev/urandom as well. It will give
better

randomness instead of the standard C thingy.

Or if your mobo have random number generator like mine use it.

if your problem is mainly speed you can try to set random numbers
array
( or file with random nubers or any other storage ) at game start
( if this array is large enough ) nobody will notice the difference
PAMASH


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl