OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_BASE_UTILS_RANDOM_NUMBER_GENERATOR_H_ | 5 #ifndef V8_BASE_UTILS_RANDOM_NUMBER_GENERATOR_H_ |
6 #define V8_BASE_UTILS_RANDOM_NUMBER_GENERATOR_H_ | 6 #define V8_BASE_UTILS_RANDOM_NUMBER_GENERATOR_H_ |
7 | 7 |
| 8 #include "src/base/base-export.h" |
8 #include "src/base/macros.h" | 9 #include "src/base/macros.h" |
9 | 10 |
10 namespace v8 { | 11 namespace v8 { |
11 namespace base { | 12 namespace base { |
12 | 13 |
13 // ----------------------------------------------------------------------------- | 14 // ----------------------------------------------------------------------------- |
14 // RandomNumberGenerator | 15 // RandomNumberGenerator |
15 | 16 |
16 // This class is used to generate a stream of pseudo-random numbers. The class | 17 // This class is used to generate a stream of pseudo-random numbers. The class |
17 // uses a 64-bit seed, which is passed through MurmurHash3 to create two 64-bit | 18 // uses a 64-bit seed, which is passed through MurmurHash3 to create two 64-bit |
18 // state values. This pair of state values is then used in xorshift128+. | 19 // state values. This pair of state values is then used in xorshift128+. |
19 // The resulting stream of pseudo-random numbers has a period length of 2^128-1. | 20 // The resulting stream of pseudo-random numbers has a period length of 2^128-1. |
20 // See Marsaglia: http://www.jstatsoft.org/v08/i14/paper | 21 // See Marsaglia: http://www.jstatsoft.org/v08/i14/paper |
21 // And Vigna: http://vigna.di.unimi.it/ftp/papers/xorshiftplus.pdf | 22 // And Vigna: http://vigna.di.unimi.it/ftp/papers/xorshiftplus.pdf |
22 // NOTE: Any changes to the algorithm must be tested against TestU01. | 23 // NOTE: Any changes to the algorithm must be tested against TestU01. |
23 // Please find instructions for this in the internal repository. | 24 // Please find instructions for this in the internal repository. |
24 | 25 |
25 // If two instances of RandomNumberGenerator are created with the same seed, and | 26 // If two instances of RandomNumberGenerator are created with the same seed, and |
26 // the same sequence of method calls is made for each, they will generate and | 27 // the same sequence of method calls is made for each, they will generate and |
27 // return identical sequences of numbers. | 28 // return identical sequences of numbers. |
28 // This class uses (probably) weak entropy by default, but it's sufficient, | 29 // This class uses (probably) weak entropy by default, but it's sufficient, |
29 // because it is the responsibility of the embedder to install an entropy source | 30 // because it is the responsibility of the embedder to install an entropy source |
30 // using v8::V8::SetEntropySource(), which provides reasonable entropy, see: | 31 // using v8::V8::SetEntropySource(), which provides reasonable entropy, see: |
31 // https://code.google.com/p/v8/issues/detail?id=2905 | 32 // https://code.google.com/p/v8/issues/detail?id=2905 |
32 // This class is neither reentrant nor threadsafe. | 33 // This class is neither reentrant nor threadsafe. |
33 | 34 |
34 class RandomNumberGenerator final { | 35 class V8_BASE_EXPORT RandomNumberGenerator final { |
35 public: | 36 public: |
36 // EntropySource is used as a callback function when V8 needs a source of | 37 // EntropySource is used as a callback function when V8 needs a source of |
37 // entropy. | 38 // entropy. |
38 typedef bool (*EntropySource)(unsigned char* buffer, size_t buflen); | 39 typedef bool (*EntropySource)(unsigned char* buffer, size_t buflen); |
39 static void SetEntropySource(EntropySource entropy_source); | 40 static void SetEntropySource(EntropySource entropy_source); |
40 | 41 |
41 RandomNumberGenerator(); | 42 RandomNumberGenerator(); |
42 explicit RandomNumberGenerator(int64_t seed) { SetSeed(seed); } | 43 explicit RandomNumberGenerator(int64_t seed) { SetSeed(seed); } |
43 | 44 |
44 // Returns the next pseudorandom, uniformly distributed int value from this | 45 // Returns the next pseudorandom, uniformly distributed int value from this |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 | 122 |
122 int64_t initial_seed_; | 123 int64_t initial_seed_; |
123 uint64_t state0_; | 124 uint64_t state0_; |
124 uint64_t state1_; | 125 uint64_t state1_; |
125 }; | 126 }; |
126 | 127 |
127 } // namespace base | 128 } // namespace base |
128 } // namespace v8 | 129 } // namespace v8 |
129 | 130 |
130 #endif // V8_BASE_UTILS_RANDOM_NUMBER_GENERATOR_H_ | 131 #endif // V8_BASE_UTILS_RANDOM_NUMBER_GENERATOR_H_ |
OLD | NEW |