question on rand()

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


On 08/06/2011 01:19 PM, Dennis Clarke wrote:
> 
>> 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

/dev/urandom, please, unless you only want a few bytes: /dev/random
is unlikely to be able to provide much random data.  If you need
/dev/random you probably know that you need it, and why.

Andrew.



More information about the Gcc-help mailing list