public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* question on rand()
@ 2011-08-06  7:47 Anna Sidera
  2011-08-06  9:08 ` Andrew Haley
  0 siblings, 1 reply; 11+ messages in thread
From: Anna Sidera @ 2011-08-06  7:47 UTC (permalink / raw)
  To: gcc-help

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.

Thanks,
Anna

^ permalink raw reply	[flat|nested] 11+ messages in thread
* Re: question on rand()
@ 2011-08-06 12:19 Dennis Clarke
  2011-08-06 16:00 ` Andrew Haley
  0 siblings, 1 reply; 11+ messages in thread
From: Dennis Clarke @ 2011-08-06 12:19 UTC (permalink / raw)
  To: Andrew Haley; +Cc: gcc-help


> 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


^ permalink raw reply	[flat|nested] 11+ messages in thread
* Re: question on rand()
@ 2011-08-06 22:45 Dennis Clarke
  0 siblings, 0 replies; 11+ messages in thread
From: Dennis Clarke @ 2011-08-06 22:45 UTC (permalink / raw)
  To: Ian Lance Taylor; +Cc: Anna Sidera, gcc-help


> Anna Sidera <sidera.anna@ucy.ac.cy> writes:
>
>>From this, can I find out when the numbers start repeating? How can I
>>> find it? I cannot find the period using
>>
>> man rand
>
> This question really doesn't have anything to do with gcc.  gcc does not
> provide a rand function.  The rand function will come from your C
> library, and gcc does not include a C library.
>
> If you are using GNU glibc, which is probably the case if you are using
> a GNU/Linux system, then rand is not implemented as a linear
> congruential generator, and the period is quite long.  See the source
> code.
>

At risk of being well and truely off topic, I suggest that prng folks look
at the "Mersenne twister" algorithm developed by Makoto Matsumoto and
Takuji Nishimura :

  http://en.wikipedia.org/wiki/Mersenne_twister

Works great.

I think that ends this topic.

-- 
--
+-------------------------+-----------------------------------+
| Dennis Clarke           | Solaris and Linux and Open Source |
| dclarke@blastwave.org   | are my passion. ( FREE == OPEN )  |
+-------------------------+-----------------------------------+


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2011-08-07 13:48 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-06  7:47 question on rand() Anna Sidera
2011-08-06  9:08 ` Andrew Haley
2011-08-06 16:34   ` Marc Glisse
2011-08-06 16:53     ` Anna Sidera
2011-08-06 18:06       ` Jeffrey Walton
2011-08-06 19:01       ` Ian Lance Taylor
2011-08-07  8:42         ` Anna Sidera
2011-08-07 13:48           ` Ian Lance Taylor
2011-08-06 12:19 Dennis Clarke
2011-08-06 16:00 ` Andrew Haley
2011-08-06 22:45 Dennis Clarke

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).