Re: [Boost-users] Newbie question: Random numbers.

I was just checking over my suggestion and realized that clock() is not a good choice for seeding the generator, especially with my optimization. It returns the amount of time since the process started, which in a static initializer for a global variable, will provide very low entropy. In a few test runs, it even returned 0, which caused an assertion failure in the random number library. An ideal solution would be to use random_device, which is defined in boost/nondet_random.hpp. You could then replace the definition of generator with something like this: //Declare the generator as a global variable for easy reuse random_device nondet_generator; base_generator_type generator(nondet_generator()); If random_device isn't implemented on your platform, then you can look into other ways to seed the generator. On my system (Windows XP running VC8) I used GetTickCount(), which returns the number of milliseconds since Windows started. As a last resort, you could call clock() after the user has entered something, either the normal data entry for your program (preferred), or a special request to hit enter. Does anybody else know of other good sources of entropy to seed the generator? I also found that the loop to seed the generator (initialize() in my version) doesn't really help, since the seed function completely replaces the current value of the generator; it doesn't combine with the previous value.
-----Original Message----- From: Andrew Holden Sent: Friday, May 19, 2006 9:18 AM To: boost-users@lists.boost.org Subject: Re: [Boost-users] Newbie question: Random numbers.
My first thought is that you probably shouldn't reinitialize the generator for every random number you need. I would recommend that you create and seed "generator" once at the start of your program, then reuse it whenever you need a random number.
Perhaps something like the following:
namespace Random { typedef boost::minstd_rand base_generator_type; typedef boost::uniform_int<> distribution_type; typedef boost::variate_generator
gen_type; //Declare the generator as a global variable for easy reuse base_generator_type generator(static_cast<unsigned long>(clock()));
/*************************************************** * initializes the random number generator * **************************************************/ void initialize() { for (int i = 0; i < 30000; ++i) generator.seed(static_cast<unsigned long>(clock())); }
/*************************************************** * generates a random number between a and b * * @param a the starting range * * @param b the integer ending range * * @return N such that a <= N <= b * **************************************************/ template <class T> T rand_int (const T& a, const T& b) { //Check the parameters BOOST_STATIC_ASSERT(boost::is_integral<T>::value); assert (b >= a);
//Set up the desired distribution gen_type ran_gen(generator, distribution_type(a, b));
//Get a random number return ran_gen(); } }
using Random::rand_int;
int main(int argc, char* argv[]) { Random::initialize();
std::cout << rand_int (1,1000) << std::endl << rand_int (1,1000) << std::endl << rand_int (1,1000) << std::endl << rand_int (1,1000) << std::endl;
return 0; } ________________________________________ From: chun ping wang [mailto:cablepuff@gmail.com] Sent: Friday, May 19, 2006 3:55 AM To: boost-users@lists.boost.org Subject: [Boost-users] Newbie question: Random numbers.
I have a simple question, is there a very fast and effecient way to generate random numbers that are fast on the fly. The numbers are really randomize and distorted...
/*************************************************** * generates a random number between a and b * * @param a the starting range * * @param b the integer ending range * * @return N such that a <= N <= b * **************************************************/ template <class T> T randint(const T& a, const T& b) { BOOST_STATIC_ASSERT(boost::is_integral<T>::value); assert (b >= a); typedef boost::minstd_rand base_generator_type; typedef boost::uniform_int<> distribution_type; typedef boost::variate_generator
gen_type; base_generator_type generator(boost::numeric_cast<unsigned long>(clock())); gen_type ran_gen(generator, distribution_type(a, b)); for (int i = 0; i < 30000; ++i) generator.seed(boost::numeric_cast<unsigned long>(clock())); return ran_gen(); } This works but is super slow... are there effective ways to do it...
Thanks.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Andrew Holden wrote:
Does anybody else know of other good sources of entropy to seed the generator?
On Windows I use QueryPerformanceCounter, GetCurrentProcessId, GetCurrentThreadId and GlobalMemoryStatus as entropy sources. OpenSSL uses the contents of the screen.

One particularly effective technique is to seed it once, and only once. Iterate (through the random stream) a bit, then save that value to a file. Anytime your program closes, just grab one last number from the stream, save it to the file, and re-load that as your next seed. That way you're using one (and only one) random sequence for all runs of your program, and since any good random number generator (e.g. Mersenne Twister, etc) is pretty close to random along an entire stream, you're doing pretty well. Re-seeding naively can be dangerous, as it's not just entropy you want in a seed - it's things like relative primality, etc. As noted, seeding with a clock isn't nearly as effective as most think. Process ID's tend to be 'low' (not 11+ bits long, at least), as do ThreadID's, and other such things. Not nearly as much entropy as many think. - Greg On May 19, 2006, at 11:08 AM, Peter Dimov wrote:
Andrew Holden wrote:
Does anybody else know of other good sources of entropy to seed the generator?
On Windows I use QueryPerformanceCounter, GetCurrentProcessId, GetCurrentThreadId and GlobalMemoryStatus as entropy sources. OpenSSL uses the contents of the screen.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Greg Link wrote:
[...] Re-seeding naively can be dangerous, as it's not just entropy you want in a seed - it's things like relative primality, etc. As noted, seeding with a clock isn't nearly as effective as most think. Process ID's tend to be 'low' (not 11+ bits long, at least), as do ThreadID's, and other such things. Not nearly as much entropy as many think.
Well, that's why one would typically combine as many entropy sources as possible using a hash function (MD5, SHA, even boost::hash if 32 bits are enough.) :-) For more demanding uses there's always CryptGenRandom, or RtlGenRandom on XP+. On VC 8.0 there's also rand_s. http://blogs.msdn.com/michael_howard/archive/2005/01/14/353379.aspx

Well, that's why one would typically combine as many entropy sources as possible using a hash function (MD5, SHA, even boost::hash if 32 bits are enough.) :-)
For more demanding uses there's always CryptGenRandom, or RtlGenRandom on XP+. On VC 8.0 there's also rand_s.
How about Mersenne Twister by math profs at Hiroshima U.: http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html

admin@geocodenet.com wrote:
Well, that's why one would typically combine as many entropy sources as possible using a hash function (MD5, SHA, even boost::hash if 32 bits are enough.) :-)
For more demanding uses there's always CryptGenRandom, or RtlGenRandom on XP+. On VC 8.0 there's also rand_s.
How about Mersenne Twister by math profs at Hiroshima U.: http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
You have to seed the Mersenne Twister with something, which is what this portion of the thread is about; a good source of entropy to obtain a seed from.

How about Mersenne Twister by math profs at Hiroshima U.: http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
You have to seed the Mersenne Twister with something, which is what this portion of the thread is about; a good source of entropy to obtain a seed from.
Yeah I just went back through the thread and saw that. Didn't Schneier give some examples of how to generate highly entropic seeds in his book? If I recall correctly, he suggested hooking up hardware to measure various properties of subatomic particles to get some truly random values. Of course it all depends on how much entropy you really need and how much you're willing to pay for it. Entropy requires no maintenance.

admin@geocodenet.com wrote:
How about Mersenne Twister by math profs at Hiroshima U.: http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html You have to seed the Mersenne Twister with something, which is what this portion of the thread is about; a good source of entropy to obtain a seed from.
Yeah I just went back through the thread and saw that. Didn't Schneier give some examples of how to generate highly entropic seeds in his book? If I recall correctly, he suggested hooking up hardware to measure various properties of subatomic particles to get some truly random values. Of course it all depends on how much entropy you really need and how much you're willing to pay for it.
Entropy requires no maintenance.
:-) A cheap method, that I've used, is to hook up a radio to your audio card and record a frequency which is sufficiently far away from real radio traffic. With some post processing, to clean up and to ensure crypto safe entropy levels, it works very well and gives gigs of entropy data quickly. -- -- Grafik - Don't Assume Anything -- Redshift Software, Inc. - http://redshift-software.com -- rrivera/acm.org - grafik/redshift-software.com -- 102708583/icq - grafikrobot/aim - grafikrobot/yahoo
participants (5)
-
admin@geocodenet.com
-
Andrew Holden
-
Greg Link
-
Peter Dimov
-
Rene Rivera