Boost random not looking so random
I'm using boost to shuffle an array, but it's not looking as random as I
would expect. Am I not using it right?
The code below outputs sequences that look random mostly, but I see
patterns such as 7 7 12 12 11.
boost::random::mt19937 rng;
boost::minstd_rand lcggen;
lcggen.seed(clockTime);
rng.seed(lcggen);
unsigned min=1;
unsigned max=15;
for(unsigned i=0;i<5;i++)
{ boost::random::uniform_int_distribution<> dist(min,max);
const int j = dist(rng);
... code that shuffles stuff using j ...
min++;
cout<
On Sat, Oct 27, 2012 at 09:30:45PM -0700, Robin Rowe wrote:
I'm using boost to shuffle an array, but it's not looking as random as I would expect. Am I not using it right?
Randomness doesn't give a hoot about discernable patterns. A generator that avoids "non-random patterns" would be properly defective. If you want to measure the randomness of something, consider using the proper statistical tests designed to measure the uncorrellation of sources.
The code below outputs sequences that look random mostly, but I see patterns such as 7 7 12 12 11.
boost::random::mt19937 rng; boost::minstd_rand lcggen; lcggen.seed(clockTime); rng.seed(lcggen); unsigned min=1; unsigned max=15; for(unsigned i=0;i<5;i++) { boost::random::uniform_int_distribution<> dist(min,max); const int j = dist(rng); ... code that shuffles stuff using j ... min++; cout<
You may of course be using the results of an uniform distribution in a way that causes the resulting shuffle have some configurations that are more likely than others, but ocular observation is still not a reliable metric for that. -- Lars Viklund | zao@acc.umu.se
I'm using boost to shuffle an array, but it's not looking as random as I would expect.
for(unsigned i=0;i<5;i++) { boost::random::uniform_int_distribution<> dist(min,max); const int j = dist(rng); ... code that shuffles stuff using j ... min++; cout<
You may of course be using the results of an uniform distribution in a way that causes the resulting shuffle have some configurations that are more likely than others...
Use std::shuffle from the algorithm header. This stuff is tricky to get right if you write it yourself. Google "Knuth shuffle" for some background. - Rhys
If you want to measure the randomness of something...
No, that wasn't my question. I just want to know if I'm using boost random correctly/optimally in my code below. Are there any programmers here who can answer that? On 10/27/2012 9:30 PM, Robin Rowe wrote:
boost::random::mt19937 rng; boost::minstd_rand lcggen; lcggen.seed(clockTime); rng.seed(lcggen); unsigned min=1; unsigned max=15; for(unsigned i=0;i<5;i++) { boost::random::uniform_int_distribution<> dist(min,max); const int j = dist(rng); ... code that shuffles stuff using j ... min++; cout<
Thanks, Robin -- www.CinePaint.org
participants (3)
-
Lars Viklund
-
Rhys Ulerich
-
Robin Rowe