@@ -82,8 +82,42 @@ BOOST_AUTO_TEST_CASE(stdrandom_test)
82
82
for (int j = 1 ; j <= 10 ; ++j) {
83
83
BOOST_CHECK (std::find (test.begin (), test.end (), j) != test.end ());
84
84
}
85
+ Shuffle (test.begin (), test.end (), ctx);
86
+ for (int j = 1 ; j <= 10 ; ++j) {
87
+ BOOST_CHECK (std::find (test.begin (), test.end (), j) != test.end ());
88
+ }
85
89
}
86
90
87
91
}
88
92
93
+ /* * Test that Shuffle reaches every permutation with equal probability. */
94
+ BOOST_AUTO_TEST_CASE (shuffle_stat_test)
95
+ {
96
+ FastRandomContext ctx (true );
97
+ uint32_t counts[5 * 5 * 5 * 5 * 5 ] = {0 };
98
+ for (int i = 0 ; i < 12000 ; ++i) {
99
+ int data[5 ] = {0 , 1 , 2 , 3 , 4 };
100
+ Shuffle (std::begin (data), std::end (data), ctx);
101
+ int pos = data[0 ] + data[1 ] * 5 + data[2 ] * 25 + data[3 ] * 125 + data[4 ] * 625 ;
102
+ ++counts[pos];
103
+ }
104
+ unsigned int sum = 0 ;
105
+ double chi_score = 0.0 ;
106
+ for (int i = 0 ; i < 5 * 5 * 5 * 5 * 5 ; ++i) {
107
+ int i1 = i % 5 , i2 = (i / 5 ) % 5 , i3 = (i / 25 ) % 5 , i4 = (i / 125 ) % 5 , i5 = i / 625 ;
108
+ uint32_t count = counts[i];
109
+ if (i1 == i2 || i1 == i3 || i1 == i4 || i1 == i5 || i2 == i3 || i2 == i4 || i2 == i5 || i3 == i4 || i3 == i5 || i4 == i5) {
110
+ BOOST_CHECK (count == 0 );
111
+ } else {
112
+ chi_score += ((count - 100.0 ) * (count - 100.0 )) / 100.0 ;
113
+ BOOST_CHECK (count > 50 );
114
+ BOOST_CHECK (count < 150 );
115
+ sum += count;
116
+ }
117
+ }
118
+ BOOST_CHECK (chi_score > 58.1411 ); // 99.9999% confidence interval
119
+ BOOST_CHECK (chi_score < 210.275 );
120
+ BOOST_CHECK_EQUAL (sum, 12000 );
121
+ }
122
+
89
123
BOOST_AUTO_TEST_SUITE_END ()
0 commit comments