This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug libstdc++/33815] New: tr1::uniform_int isn't uniform
- From: "jkherciueh at gmx dot net" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 19 Oct 2007 04:34:35 -0000
- Subject: [Bug libstdc++/33815] New: tr1::uniform_int isn't uniform
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
Consider:
#include <ctime>
#include <iostream>
#include <vector>
#include <tr1/random>
void test ( unsigned int num_buckets, unsigned int seed = 3141592 ) {
std::cout << "using " << num_buckets << " buckets:\n";
std::tr1::mt19937 eng(seed);
const unsigned long range = eng.max() / 5.5;
std::tr1::uniform_int<unsigned long> rnd( 0, range );
std::vector<unsigned long> bucket ( num_buckets, 0 );
for ( unsigned int i = 0; i < 1000000; ++i ) {
++bucket[ rnd(eng) / (range / num_buckets) ];
}
for ( unsigned int i = 0; i < num_buckets; ++i ) {
std::cout << i << ": " << bucket[i] << '\n';
}
std::cout << '\n';
}
int main ( void ) {
test( 2 );
test( 3 );
test( 4 );
test( 6 );
test( 10 );
test( 20 );
test( 30 );
test( 40 );
}
It yields:
using 2 buckets.
0: 545389
1: 454611
using 3 buckets.
0: 363350
1: 333829
2: 302821
using 4 buckets.
0: 272507
1: 272882
2: 227489
3: 227122
using 6 buckets.
0: 181743
1: 181607
2: 182039
3: 151790
4: 151182
5: 151639
...
This doesn't look uniform. It looks more like the buckets in the
lower half get hit significantly more often.
Credit: This strange behavior had been reported by David Benbennick
to Boost long ago and was apparently never fixed.
--
Summary: tr1::uniform_int isn't uniform
Product: gcc
Version: 4.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: jkherciueh at gmx dot net
GCC build triplet: i686-pc-linux
GCC host triplet: i686-pc-linux
GCC target triplet: i686-pc-linux
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33815