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