Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(168)

Side by Side Diff: test/cctest/test-random-number-generator.cc

Issue 1467133006: Add test to check PRNG quality. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: correctly set up random seed Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | test/mjsunit/random-bit-correlations.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 the V8 project authors. All rights reserved. 1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 FLAG_random_seed = static_cast<int>(kRandomSeeds[n]); 44 FLAG_random_seed = static_cast<int>(kRandomSeeds[n]);
45 v8::Isolate::CreateParams create_params; 45 v8::Isolate::CreateParams create_params;
46 create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); 46 create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
47 v8::Isolate* i = v8::Isolate::New(create_params); 47 v8::Isolate* i = v8::Isolate::New(create_params);
48 v8::base::RandomNumberGenerator& rng = 48 v8::base::RandomNumberGenerator& rng =
49 *reinterpret_cast<Isolate*>(i)->random_number_generator(); 49 *reinterpret_cast<Isolate*>(i)->random_number_generator();
50 CHECK_EQ(kRandomSeeds[n], rng.initial_seed()); 50 CHECK_EQ(kRandomSeeds[n], rng.initial_seed());
51 i->Dispose(); 51 i->Dispose();
52 } 52 }
53 } 53 }
54
55
56 // Chi squared for getting m 0s out of n bits.
57 double ChiSquared(int m, int n) {
58 double ys_minus_np1 = (m - n / 2.0);
59 double chi_squared_1 = ys_minus_np1 * ys_minus_np1 * 2.0 / n;
60 double ys_minus_np2 = ((n - m) - n / 2.0);
61 double chi_squared_2 = ys_minus_np2 * ys_minus_np2 * 2.0 / n;
62 return chi_squared_1 + chi_squared_2;
63 }
64
65
66 // Test for correlations between recent bits from the PRNG, or bits that are
67 // biased.
68 void RandomBitCorrelation(int random_bit) {
69 FLAG_random_seed = 31415926;
70 v8::Isolate::CreateParams create_params;
71 create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
72 v8::Isolate* isolate = v8::Isolate::New(create_params);
73 Isolate* i_isolate = reinterpret_cast<Isolate*>(isolate);
74 v8::base::RandomNumberGenerator* rng = i_isolate->random_number_generator();
75 #ifdef DEBUG
76 const int kHistory = 2;
77 const int kRepeats = 1000;
78 #else
79 const int kHistory = 8;
80 const int kRepeats = 10000;
81 #endif
82 uint32_t history[kHistory];
83 // The predictor bit is either constant 0 or 1, or one of the bits from the
84 // history.
85 for (int predictor_bit = -2; predictor_bit < 32; predictor_bit++) {
86 // The predicted bit is one of the bits from the PRNG.
87 for (int ago = 0; ago < kHistory; ago++) {
88 // We don't want to check whether each bit predicts itself.
89 if (ago == 0 && predictor_bit == random_bit) continue;
90
91 // Enter the new random value into the history
92 for (int i = ago; i >= 0; i--) {
93 history[i] = bit_cast<uint32_t>(rng->NextInt());
94 }
95
96 // Find out how many of the bits are the same as the prediction bit.
97 int m = 0;
98 for (int i = 0; i < kRepeats; i++) {
99 v8::HandleScope scope(isolate);
100 uint32_t random = bit_cast<uint32_t>(rng->NextInt());
101 for (int j = ago - 1; j >= 0; j--) history[j + 1] = history[j];
102 history[0] = random;
103
104 int predicted;
105 if (predictor_bit >= 0) {
106 predicted = (history[ago] >> predictor_bit) & 1;
107 } else {
108 predicted = predictor_bit == -2 ? 0 : 1;
109 }
110 int bit = (random >> random_bit) & 1;
111 if (bit == predicted) m++;
112 }
113
114 // Chi squared analysis for k = 2 (2, states: same/not-same) and one
115 // degree of freedom (k - 1).
116 double chi_squared = ChiSquared(m, kRepeats);
117 if (chi_squared > 24) {
118 int percent = static_cast<int>(m * 100.0 / kRepeats);
119 if (predictor_bit < 0) {
120 PrintF("Bit %d is %d %d%% of the time\n", random_bit,
121 predictor_bit == -2 ? 0 : 1, percent);
122 } else {
123 PrintF("Bit %d is the same as bit %d %d ago %d%% of the time\n",
124 random_bit, predictor_bit, ago, percent);
125 }
126 }
127
128 // For 1 degree of freedom this corresponds to 1 in a million. We are
129 // running ~8000 tests, so that would be surprising.
130 CHECK(chi_squared <= 24);
131
132 // If the predictor bit is a fixed 0 or 1 then it makes no sense to
133 // repeat the test with a different age.
134 if (predictor_bit < 0) break;
135 }
136 }
137 isolate->Dispose();
138 }
139
140
141 #define TEST_RANDOM_BIT(BIT) \
142 TEST(RandomBitCorrelations##BIT) { RandomBitCorrelation(BIT); }
143
144 TEST_RANDOM_BIT(0)
145 TEST_RANDOM_BIT(1)
146 TEST_RANDOM_BIT(2)
147 TEST_RANDOM_BIT(3)
148 TEST_RANDOM_BIT(4)
149 TEST_RANDOM_BIT(5)
150 TEST_RANDOM_BIT(6)
151 TEST_RANDOM_BIT(7)
152 TEST_RANDOM_BIT(8)
153 TEST_RANDOM_BIT(9)
154 TEST_RANDOM_BIT(10)
155 TEST_RANDOM_BIT(11)
156 TEST_RANDOM_BIT(12)
157 TEST_RANDOM_BIT(13)
158 TEST_RANDOM_BIT(14)
159 TEST_RANDOM_BIT(15)
160 TEST_RANDOM_BIT(16)
161 TEST_RANDOM_BIT(17)
162 TEST_RANDOM_BIT(18)
163 TEST_RANDOM_BIT(19)
164 TEST_RANDOM_BIT(20)
165 TEST_RANDOM_BIT(21)
166 TEST_RANDOM_BIT(22)
167 TEST_RANDOM_BIT(23)
168 TEST_RANDOM_BIT(24)
169 TEST_RANDOM_BIT(25)
170 TEST_RANDOM_BIT(26)
171 TEST_RANDOM_BIT(27)
172 TEST_RANDOM_BIT(28)
173 TEST_RANDOM_BIT(29)
174 TEST_RANDOM_BIT(30)
175 TEST_RANDOM_BIT(31)
176
177 #undef TEST_RANDOM_BIT
OLDNEW
« no previous file with comments | « no previous file | test/mjsunit/random-bit-correlations.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698