OLD | NEW |
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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 static void SetEntropySource(EntropySource entropy_source); | 52 static void SetEntropySource(EntropySource entropy_source); |
53 | 53 |
54 RandomNumberGenerator(); | 54 RandomNumberGenerator(); |
55 explicit RandomNumberGenerator(int64_t seed) { SetSeed(seed); } | 55 explicit RandomNumberGenerator(int64_t seed) { SetSeed(seed); } |
56 | 56 |
57 // Returns the next pseudorandom, uniformly distributed int value from this | 57 // Returns the next pseudorandom, uniformly distributed int value from this |
58 // random number generator's sequence. The general contract of |NextInt()| is | 58 // random number generator's sequence. The general contract of |NextInt()| is |
59 // that one int value is pseudorandomly generated and returned. | 59 // that one int value is pseudorandomly generated and returned. |
60 // All 2^32 possible integer values are produced with (approximately) equal | 60 // All 2^32 possible integer values are produced with (approximately) equal |
61 // probability. | 61 // probability. |
62 V8_INLINE(int NextInt()) V8_WARN_UNUSED_RESULT { | 62 V8_INLINE int NextInt() V8_WARN_UNUSED_RESULT { |
63 return Next(32); | 63 return Next(32); |
64 } | 64 } |
65 | 65 |
66 // Returns a pseudorandom, uniformly distributed int value between 0 | 66 // Returns a pseudorandom, uniformly distributed int value between 0 |
67 // (inclusive) and the specified max value (exclusive), drawn from this random | 67 // (inclusive) and the specified max value (exclusive), drawn from this random |
68 // number generator's sequence. The general contract of |NextInt(int)| is that | 68 // number generator's sequence. The general contract of |NextInt(int)| is that |
69 // one int value in the specified range is pseudorandomly generated and | 69 // one int value in the specified range is pseudorandomly generated and |
70 // returned. All max possible int values are produced with (approximately) | 70 // returned. All max possible int values are produced with (approximately) |
71 // equal probability. | 71 // equal probability. |
72 int NextInt(int max) V8_WARN_UNUSED_RESULT; | 72 int NextInt(int max) V8_WARN_UNUSED_RESULT; |
73 | 73 |
74 // Returns the next pseudorandom, uniformly distributed boolean value from | 74 // Returns the next pseudorandom, uniformly distributed boolean value from |
75 // this random number generator's sequence. The general contract of | 75 // this random number generator's sequence. The general contract of |
76 // |NextBoolean()| is that one boolean value is pseudorandomly generated and | 76 // |NextBoolean()| is that one boolean value is pseudorandomly generated and |
77 // returned. The values true and false are produced with (approximately) equal | 77 // returned. The values true and false are produced with (approximately) equal |
78 // probability. | 78 // probability. |
79 V8_INLINE(bool NextBool()) V8_WARN_UNUSED_RESULT { | 79 V8_INLINE bool NextBool() V8_WARN_UNUSED_RESULT { |
80 return Next(1) != 0; | 80 return Next(1) != 0; |
81 } | 81 } |
82 | 82 |
83 // Returns the next pseudorandom, uniformly distributed double value between | 83 // Returns the next pseudorandom, uniformly distributed double value between |
84 // 0.0 and 1.0 from this random number generator's sequence. | 84 // 0.0 and 1.0 from this random number generator's sequence. |
85 // The general contract of |NextDouble()| is that one double value, chosen | 85 // The general contract of |NextDouble()| is that one double value, chosen |
86 // (approximately) uniformly from the range 0.0 (inclusive) to 1.0 | 86 // (approximately) uniformly from the range 0.0 (inclusive) to 1.0 |
87 // (exclusive), is pseudorandomly generated and returned. | 87 // (exclusive), is pseudorandomly generated and returned. |
88 double NextDouble() V8_WARN_UNUSED_RESULT; | 88 double NextDouble() V8_WARN_UNUSED_RESULT; |
89 | 89 |
90 // Fills the elements of a specified array of bytes with random numbers. | 90 // Fills the elements of a specified array of bytes with random numbers. |
91 void NextBytes(void* buffer, size_t buflen); | 91 void NextBytes(void* buffer, size_t buflen); |
92 | 92 |
93 private: | 93 private: |
94 static const int64_t kMultiplier = V8_2PART_UINT64_C(0x5, deece66d); | 94 static const int64_t kMultiplier = V8_2PART_UINT64_C(0x5, deece66d); |
95 static const int64_t kAddend = 0xb; | 95 static const int64_t kAddend = 0xb; |
96 static const int64_t kMask = V8_2PART_UINT64_C(0xffff, ffffffff); | 96 static const int64_t kMask = V8_2PART_UINT64_C(0xffff, ffffffff); |
97 | 97 |
98 int Next(int bits) V8_WARN_UNUSED_RESULT; | 98 int Next(int bits) V8_WARN_UNUSED_RESULT; |
99 void SetSeed(int64_t seed); | 99 void SetSeed(int64_t seed); |
100 | 100 |
101 int64_t seed_; | 101 int64_t seed_; |
102 }; | 102 }; |
103 | 103 |
104 } } // namespace v8::internal | 104 } } // namespace v8::internal |
105 | 105 |
106 #endif // V8_UTILS_RANDOM_NUMBER_GENERATOR_H_ | 106 #endif // V8_UTILS_RANDOM_NUMBER_GENERATOR_H_ |
OLD | NEW |