mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-11 22:50:59 +01:00
Introduce a Shuffle for FastRandomContext and use it in wallet and coinselection
This commit is contained in:
@@ -75,8 +75,42 @@ BOOST_AUTO_TEST_CASE(stdrandom_test)
|
||||
for (int j = 1; j <= 10; ++j) {
|
||||
BOOST_CHECK(std::find(test.begin(), test.end(), j) != test.end());
|
||||
}
|
||||
Shuffle(test.begin(), test.end(), ctx);
|
||||
for (int j = 1; j <= 10; ++j) {
|
||||
BOOST_CHECK(std::find(test.begin(), test.end(), j) != test.end());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** Test that Shuffle reaches every permutation with equal probability. */
|
||||
BOOST_AUTO_TEST_CASE(shuffle_stat_test)
|
||||
{
|
||||
FastRandomContext ctx(true);
|
||||
uint32_t counts[5 * 5 * 5 * 5 * 5] = {0};
|
||||
for (int i = 0; i < 12000; ++i) {
|
||||
int data[5] = {0, 1, 2, 3, 4};
|
||||
Shuffle(std::begin(data), std::end(data), ctx);
|
||||
int pos = data[0] + data[1] * 5 + data[2] * 25 + data[3] * 125 + data[4] * 625;
|
||||
++counts[pos];
|
||||
}
|
||||
unsigned int sum = 0;
|
||||
double chi_score = 0.0;
|
||||
for (int i = 0; i < 5 * 5 * 5 * 5 * 5; ++i) {
|
||||
int i1 = i % 5, i2 = (i / 5) % 5, i3 = (i / 25) % 5, i4 = (i / 125) % 5, i5 = i / 625;
|
||||
uint32_t count = counts[i];
|
||||
if (i1 == i2 || i1 == i3 || i1 == i4 || i1 == i5 || i2 == i3 || i2 == i4 || i2 == i5 || i3 == i4 || i3 == i5 || i4 == i5) {
|
||||
BOOST_CHECK(count == 0);
|
||||
} else {
|
||||
chi_score += ((count - 100.0) * (count - 100.0)) / 100.0;
|
||||
BOOST_CHECK(count > 50);
|
||||
BOOST_CHECK(count < 150);
|
||||
sum += count;
|
||||
}
|
||||
}
|
||||
BOOST_CHECK(chi_score > 58.1411); // 99.9999% confidence interval
|
||||
BOOST_CHECK(chi_score < 210.275);
|
||||
BOOST_CHECK_EQUAL(sum, 12000);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
Reference in New Issue
Block a user