[openbeos] Re: is rand() really random?

  • From: Michael Phipps <mphipps1@xxxxxxxxxxxxxxxx>
  • To: openbeos@xxxxxxxxxxxxx
  • Date: Fri, 09 Apr 2004 19:05:31 -0400

Scott is both correct and ... overcautious at the same time.

A lot of it depends on your needs. If you are trying to do some ultra secure, 
or ultra simple (guess a number between 1 and 10), then you probably need 
something more random than just srand(time(NULL)). But, honestly, for most 
uses, current time is fine. 

If you don't mind being BeOS specific, you could do something like this 
(untested):

srand(real_time_clock_usecs() & 0xffffffff);

This will take the current time in microseconds and take the lowest 32 bits. 
That ought to be pretty random. 

Michael

On 2004-04-09 at 17:53:01 [-0400], Scott Donaldson wrote:
> When you seed the rng use something decently random, not just time.
> 
> If you can, take time in seconds and divide by the lower bits of the tsc (if 
> the cpu has one) that should mix things a bit better.
> 
> bye, scott
> 
> >From: "Andre Stark" <An.Stark@xxxxxx>
> >Reply-To: openbeos@xxxxxxxxxxxxx
> >To: openbeos@xxxxxxxxxxxxx
> >Subject: [openbeos] is rand() really random?
> >Date: Fri, 09 Apr 2004 23:18:44 +0200 CEST
> >
> >Hello there,
> >this is not really an OBOS problem, but a BeOS one. Everytime i execute the 
> >following code:
> >
> >for (int i = 0; i < 100; i++)
> >        cout << "Random:" << rand() <<endl;
> >
> >the resulting values are exactly the same? Until now i do'nt know much about 
> >C++ programming, but normally random numbers are generated by including 
> >system time in some kind. How does this work on BeOS?

Other related posts: