MCPcopy Create free account
hub / github.com/beefytech/Beef / TestNextRandom

Function TestNextRandom

BeefRT/gperftools/src/tests/sampler_test.cc:223–252  ·  view source on GitHub ↗

Testing that NextRandom generates uniform random numbers. Applies the Anderson-Darling test for uniformity

Source from the content-addressed store, hash-verified

221// random numbers.
222// Applies the Anderson-Darling test for uniformity
223void TestNextRandom(int n) {
224 tcmalloc::Sampler sampler;
225 sampler.Init(1);
226 uint64_t x = 1;
227 // This assumes that the prng returns 48 bit numbers
228 uint64_t max_prng_value = static_cast<uint64_t>(1)<<48;
229 // Initialize
230 for (int i = 1; i <= 20; i++) { // 20 mimics sampler.Init()
231 x = sampler.NextRandom(x);
232 }
233 scoped_array<uint64_t> int_random_sample(new uint64_t[n]);
234 // Collect samples
235 for (int i = 0; i < n; i++) {
236 int_random_sample[i] = x;
237 x = sampler.NextRandom(x);
238 }
239 // First sort them...
240 sort(int_random_sample.get(), int_random_sample.get() + n);
241 scoped_array<double> random_sample(new double[n]);
242 // Convert them to uniform randoms (in the range [0,1])
243 for (int i = 0; i < n; i++) {
244 random_sample[i] = static_cast<double>(int_random_sample[i])/max_prng_value;
245 }
246 // Now compute the Anderson-Darling statistic
247 double ad_pvalue = AndersonDarlingTest(n, random_sample.get());
248 LOG(INFO) << StringPrintf("pvalue for AndersonDarlingTest "
249 "with n= %d is p= %f\n", n, ad_pvalue);
250 CHECK_GT(min(ad_pvalue, 1 - ad_pvalue), 0.0001);
251 // << StringPrintf("prng is not uniform, %d\n", n);
252}
253
254
255TEST(Sampler, TestNextRandom_MultipleValues) {

Callers 1

TESTFunction · 0.70

Calls 6

AndersonDarlingTestFunction · 0.70
StringPrintfFunction · 0.70
LOGFunction · 0.50
InitMethod · 0.45
NextRandomMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected