Boost logo

Boost Users :

Subject: Re: [Boost-users] Random: How to get a different seed?
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2011-01-24 16:11:55


AMDG

On 1/24/2011 12:57 PM, Lasse Kliemann wrote:
>
> I use /dev/urandom for this, like so (code fragments):
>
> FILE *dev_urandom;
> uint32_t seed;
> if((dev_urandom = fopen("/dev/urandom", "r")) == NULL) throw /* error */
> if(fread(&seed, sizeof(seed), 1, dev_urandom) == 0) throw /* error */
> if(fclose(dev_urandom) != 0) throw /* error */
> boost::mt19937 gen(seed);
>
> But maybe there are even better ways.
>

boost::random_device seed_gen;
boost::mt19937 gen(seed_gen());

should have the same effect and is more portable.

In Christ,
Steven Watanabe


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