| 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/macros.h" | 8 #include "src/base/macros.h" |
| 9 | 9 |
| 10 namespace v8 { | 10 namespace v8 { |
| 11 namespace base { | 11 namespace base { |
| 12 | 12 |
| 13 // ----------------------------------------------------------------------------- | 13 // ----------------------------------------------------------------------------- |
| 14 // RandomNumberGenerator | 14 // RandomNumberGenerator |
| 15 // | 15 // |
| 16 // This class is used to generate a stream of pseudorandom numbers. The class | 16 // This class is used to generate a stream of pseudorandom numbers. The class |
| 17 // uses a 48-bit seed, which is modified using a linear congruential formula. | 17 // uses a 48-bit seed, which is modified using a linear congruential formula. |
| 18 // (See Donald Knuth, The Art of Computer Programming, Volume 3, Section 3.2.1.) | 18 // (See Donald Knuth, The Art of Computer Programming, Volume 3, Section 3.2.1.) |
| 19 // If two instances of RandomNumberGenerator are created with the same seed, and | 19 // If two instances of RandomNumberGenerator are created with the same seed, and |
| 20 // the same sequence of method calls is made for each, they will generate and | 20 // the same sequence of method calls is made for each, they will generate and |
| 21 // return identical sequences of numbers. | 21 // return identical sequences of numbers. |
| 22 // This class uses (probably) weak entropy by default, but it's sufficient, | 22 // This class uses (probably) weak entropy by default, but it's sufficient, |
| 23 // because it is the responsibility of the embedder to install an entropy source | 23 // because it is the responsibility of the embedder to install an entropy source |
| 24 // using v8::V8::SetEntropySource(), which provides reasonable entropy, see: | 24 // using v8::V8::SetEntropySource(), which provides reasonable entropy, see: |
| 25 // https://code.google.com/p/v8/issues/detail?id=2905 | 25 // https://code.google.com/p/v8/issues/detail?id=2905 |
| 26 // This class is neither reentrant nor threadsafe. | 26 // This class is neither reentrant nor threadsafe. |
| 27 | 27 |
| 28 class RandomNumberGenerator FINAL { | 28 class RandomNumberGenerator final { |
| 29 public: | 29 public: |
| 30 // EntropySource is used as a callback function when V8 needs a source of | 30 // EntropySource is used as a callback function when V8 needs a source of |
| 31 // entropy. | 31 // entropy. |
| 32 typedef bool (*EntropySource)(unsigned char* buffer, size_t buflen); | 32 typedef bool (*EntropySource)(unsigned char* buffer, size_t buflen); |
| 33 static void SetEntropySource(EntropySource entropy_source); | 33 static void SetEntropySource(EntropySource entropy_source); |
| 34 | 34 |
| 35 RandomNumberGenerator(); | 35 RandomNumberGenerator(); |
| 36 explicit RandomNumberGenerator(int64_t seed) { SetSeed(seed); } | 36 explicit RandomNumberGenerator(int64_t seed) { SetSeed(seed); } |
| 37 | 37 |
| 38 // Returns the next pseudorandom, uniformly distributed int value from this | 38 // Returns the next pseudorandom, uniformly distributed int value from this |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 | 90 |
| 91 int Next(int bits) WARN_UNUSED_RESULT; | 91 int Next(int bits) WARN_UNUSED_RESULT; |
| 92 | 92 |
| 93 int64_t initial_seed_; | 93 int64_t initial_seed_; |
| 94 int64_t seed_; | 94 int64_t seed_; |
| 95 }; | 95 }; |
| 96 | 96 |
| 97 } } // namespace v8::base | 97 } } // namespace v8::base |
| 98 | 98 |
| 99 #endif // V8_BASE_UTILS_RANDOM_NUMBER_GENERATOR_H_ | 99 #endif // V8_BASE_UTILS_RANDOM_NUMBER_GENERATOR_H_ |
| OLD | NEW |