question on rand()

Andrew Haley aph@redhat.com
Sat Aug 6 09:08:00 GMT 2011


On 08/06/2011 08:47 AM, Anna Sidera wrote:
> Hello,
> 
> I am using gcc on a unix machine. Can you tell me how many random numbers can be generated using
> 
> rand()
> 
> before the random numbers start repeating? I found that RAND_MAX is equal to 2147483647.

One would hope it has full period, i.e. it generates all RAND_MAX numbers,
but that depends on your system.

It's perhaps not a good idea to depend on the system's generator.  A decent
32-bit one is

unsigned int xor-generator()
{
   static unsigned int y=2463534242;
   y^=(y<<13); y^=(y>>17); return (y^=(y<<5));
}

Andrew.

http://www.jstatsoft.org/v08/i14/paper



More information about the Gcc-help mailing list