| 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 V8_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 |
| 39 // random number generator's sequence. The general contract of |NextInt()| is | 39 // random number generator's sequence. The general contract of |NextInt()| is |
| 40 // that one int value is pseudorandomly generated and returned. | 40 // that one int value is pseudorandomly generated and returned. |
| 41 // All 2^32 possible integer values are produced with (approximately) equal | 41 // All 2^32 possible integer values are produced with (approximately) equal |
| 42 // probability. | 42 // probability. |
| 43 V8_INLINE int NextInt() V8_WARN_UNUSED_RESULT { | 43 V8_INLINE int NextInt() WARN_UNUSED_RESULT { |
| 44 return Next(32); | 44 return Next(32); |
| 45 } | 45 } |
| 46 | 46 |
| 47 // Returns a pseudorandom, uniformly distributed int value between 0 | 47 // Returns a pseudorandom, uniformly distributed int value between 0 |
| 48 // (inclusive) and the specified max value (exclusive), drawn from this random | 48 // (inclusive) and the specified max value (exclusive), drawn from this random |
| 49 // number generator's sequence. The general contract of |NextInt(int)| is that | 49 // number generator's sequence. The general contract of |NextInt(int)| is that |
| 50 // one int value in the specified range is pseudorandomly generated and | 50 // one int value in the specified range is pseudorandomly generated and |
| 51 // returned. All max possible int values are produced with (approximately) | 51 // returned. All max possible int values are produced with (approximately) |
| 52 // equal probability. | 52 // equal probability. |
| 53 int NextInt(int max) V8_WARN_UNUSED_RESULT; | 53 int NextInt(int max) WARN_UNUSED_RESULT; |
| 54 | 54 |
| 55 // Returns the next pseudorandom, uniformly distributed boolean value from | 55 // Returns the next pseudorandom, uniformly distributed boolean value from |
| 56 // this random number generator's sequence. The general contract of | 56 // this random number generator's sequence. The general contract of |
| 57 // |NextBoolean()| is that one boolean value is pseudorandomly generated and | 57 // |NextBoolean()| is that one boolean value is pseudorandomly generated and |
| 58 // returned. The values true and false are produced with (approximately) equal | 58 // returned. The values true and false are produced with (approximately) equal |
| 59 // probability. | 59 // probability. |
| 60 V8_INLINE bool NextBool() V8_WARN_UNUSED_RESULT { | 60 V8_INLINE bool NextBool() WARN_UNUSED_RESULT { |
| 61 return Next(1) != 0; | 61 return Next(1) != 0; |
| 62 } | 62 } |
| 63 | 63 |
| 64 // Returns the next pseudorandom, uniformly distributed double value between | 64 // Returns the next pseudorandom, uniformly distributed double value between |
| 65 // 0.0 and 1.0 from this random number generator's sequence. | 65 // 0.0 and 1.0 from this random number generator's sequence. |
| 66 // The general contract of |NextDouble()| is that one double value, chosen | 66 // The general contract of |NextDouble()| is that one double value, chosen |
| 67 // (approximately) uniformly from the range 0.0 (inclusive) to 1.0 | 67 // (approximately) uniformly from the range 0.0 (inclusive) to 1.0 |
| 68 // (exclusive), is pseudorandomly generated and returned. | 68 // (exclusive), is pseudorandomly generated and returned. |
| 69 double NextDouble() V8_WARN_UNUSED_RESULT; | 69 double NextDouble() WARN_UNUSED_RESULT; |
| 70 | 70 |
| 71 // Fills the elements of a specified array of bytes with random numbers. | 71 // Fills the elements of a specified array of bytes with random numbers. |
| 72 void NextBytes(void* buffer, size_t buflen); | 72 void NextBytes(void* buffer, size_t buflen); |
| 73 | 73 |
| 74 // Override the current ssed. | 74 // Override the current ssed. |
| 75 void SetSeed(int64_t seed); | 75 void SetSeed(int64_t seed); |
| 76 | 76 |
| 77 private: | 77 private: |
| 78 static const int64_t kMultiplier = V8_2PART_UINT64_C(0x5, deece66d); | 78 static const int64_t kMultiplier = V8_2PART_UINT64_C(0x5, deece66d); |
| 79 static const int64_t kAddend = 0xb; | 79 static const int64_t kAddend = 0xb; |
| 80 static const int64_t kMask = V8_2PART_UINT64_C(0xffff, ffffffff); | 80 static const int64_t kMask = V8_2PART_UINT64_C(0xffff, ffffffff); |
| 81 | 81 |
| 82 int Next(int bits) V8_WARN_UNUSED_RESULT; | 82 int Next(int bits) WARN_UNUSED_RESULT; |
| 83 | 83 |
| 84 int64_t seed_; | 84 int64_t seed_; |
| 85 }; | 85 }; |
| 86 | 86 |
| 87 } } // namespace v8::base | 87 } } // namespace v8::base |
| 88 | 88 |
| 89 #endif // V8_BASE_UTILS_RANDOM_NUMBER_GENERATOR_H_ | 89 #endif // V8_BASE_UTILS_RANDOM_NUMBER_GENERATOR_H_ |
| OLD | NEW |