Boost logo

Boost Users :

Subject: Re: [Boost-users] [Random] Good Truly-Random Seed
From: peterbarnabus (peterbarnabus_at_[hidden])
Date: 2010-04-21 14:04:14


On 21 Apr 2010, at 18:07, Scott McMurray wrote:

> On 21 April 2010 13:47, Marius <letto2_at_[hidden]> wrote:
>>
>> May I suggest that boost should have a cross-platform function to so this.
>> time(NULL) is not random.
>>
>
> It does, in the form of random_device, though for some reason it seems
> the Windows implementation still hasn't happened.

Yes, /dev/random which you will find in the random_device does this job well, and works fine for me on both linux and osx (dont know about windows). Its aim is to be a good source of random and unpredictable data, although its quality could vary with the amount and kind of random sources available to the kernel (eg. keystrokes). Read the man pages for details.

What you mean by a good random seed depends on the application. In many cases it is sufficient to use the time (number of seconds since the epoch) as a seed (in C++ you can get the current time with std::time(0) using ctime). But if you use std::time(0) many times in quick succession it wont provide a good seed and can even return the same values if you call it quick enough (intervals less than a second). If you are running your program many times and need a new seed each time, then another strategy could be to save the state of your random generator when you have finished and use that state to initialise your program for the next run. Then your random numbers would only start to repeat after the period of the generator (which for most of them should be much longer than you need).

If this is no good, you need hardware random number generators, of which there are many. You could also have a look at random.org.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net