question on rand()

Dennis Clarke dclarke@blastwave.org
Sat Aug 6 12:19: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));
> }
>


If the system has /dev/random then it would be better to read bytes from
that. Provided it is crypto quality. ymmv

Dennis




More information about the Gcc-help mailing list