Ticket #6801: mersenne_twister_bug.cpp

File mersenne_twister_bug.cpp, 1.1 KB (added by James Hirschorn <james.hirschorn@…>, 11 years ago)

example source code

Line 
1#include <iostream>
2#include <sstream>
3using namespace std;
4
5#include <boost/random/mersenne_twister.hpp>
6using namespace boost::random;
7
8int main(int argc, char * argv[])
9{
10 typedef mt11213b Engine;
11
12 // an MT engine with the default seed
13 Engine boost_mt;
14
15 stringstream boost_ss;
16
17 boost_ss << boost_mt;
18
19 typedef Engine::result_type UIntType;
20
21 static const size_t n = Engine::state_size;
22
23 UIntType x[n], * y = x;
24 for (size_t i = 0; i < n; ++i)
25 boost_ss >> x[i] >> ws;
26
27 Engine boost_mtOther;
28 // alter the high w - r bits of y[0] (for mt11213b, w = 32 and r = 19)
29 y[0] |= 0x00030000;
30 boost_mtOther.seed(y, y + n);
31
32 // operator== returns false
33 cout << "boost_mt == boost_mtOther: " << (boost_mt == boost_mtOther) << endl;
34
35 const size_t boost_testSize = 1000000;
36
37 // however the two engines are equivalent:
38 for (size_t i = 0; i < boost_testSize; ++i)
39 {
40 UIntType result = boost_mt();
41 UIntType resultOther = boost_mtOther();
42 if (result != resultOther)
43 cout << "critical check boost_mt() == boost_mtOther() ["
44 << result << " != " << resultOther << "] failed at iteration " << i << endl;
45 }
46}