32 #include <gtest/gtest.h>
49 using std::uniform_int_distribution;
54 TEST(random_tests, uints)
57 for (
int i = 1; i < 5; i++)
60 r32 = RandomEngine::rand_uint32();
61 cout <<
"Rand uint32_t: " << r32 << endl;
65 for (
int i = 1; i < 5; i++)
68 RandomEngine::rand_bytes(&r64,
sizeof(r64));
69 cout <<
"Rand uint64_t: " << r64 << endl;
74 for (
int i = 1; i < 5; i++)
78 cout <<
"Rand operator(): " << r32 << endl;
83 TEST(random_tests, seed)
85 string seed1(
"this is a random seed for the generator");
87 string seed2(
"this is a different random seed for the generator");
90 RandomEngine::seed(seed1);
91 auto r1 = RandomEngine::rand_uint64();
92 cout <<
"rand with seed1: " << r1 << endl;
94 RandomEngine::random_seed();
95 auto r2 = RandomEngine::rand_uint64();
96 cout <<
"rand with random_seed: " << r2 << endl;
99 RandomEngine::seed(seed2);
100 auto r3 = RandomEngine::rand_uint64();
101 cout <<
"rand with seed2: " << r3 << endl;
106 RandomEngine::seed(seed1);
107 auto x1 = RandomEngine::rand_uint64();
108 cout <<
"rand with seed1: " << x1 << endl;
110 RandomEngine::seed(seed2);
111 auto x2 = RandomEngine::rand_uint64();
112 cout <<
"rand with seed2: " << x2 << endl;
121 TEST(random_tests, mersenne_twister_engine)
125 uniform_int_distribution<int> dis(0, 9);
127 for (
int n = 0; n < 100000; ++n)
131 cout <<
"mersenne twister 100000 values 0 to 9 distribution:" << endl;
134 cout << p.first <<
" : " << p.second << endl;;
136 ASSERT_EQ(hist.size(), 10);
137 for (
int i = 0; i < 10; i++)
139 ASSERT_NE(hist.find(i), hist.end());
140 ASSERT_GT(hist[i], 0);
144 TEST(random_tests, bytes_sanity)
147 RandomEngine::rand_bytes(by, 5);
148 cout <<
"tbytes " << 5 <<
": ";
149 cout << std::hex << (uint16_t)by[0] << std::dec <<
" ";
152 for (
int i = 1; i < 5; i++)
154 if (by[i] == b) same++;
155 cout << std::hex << (uint16_t)by[i] << std::dec <<
" ";
157 cout <<
" same as byte[0]=" << same;
The random number generator is initialized with random entropy on first use.
TEST(inet_example, client_server_stream_test)
[Inet client server]