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 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 | 92 |
93 // We cannot assume that random() or rand() were seeded | 93 // We cannot assume that random() or rand() were seeded |
94 // properly, so instead of relying on random() or rand(), | 94 // properly, so instead of relying on random() or rand(), |
95 // we just seed our PRNG using timing data as fallback. | 95 // we just seed our PRNG using timing data as fallback. |
96 // This is weak entropy, but it's sufficient, because | 96 // This is weak entropy, but it's sufficient, because |
97 // it is the responsibility of the embedder to install | 97 // it is the responsibility of the embedder to install |
98 // an entropy source using v8::V8::SetEntropySource(), | 98 // an entropy source using v8::V8::SetEntropySource(), |
99 // which provides reasonable entropy, see: | 99 // which provides reasonable entropy, see: |
100 // https://code.google.com/p/v8/issues/detail?id=2905 | 100 // https://code.google.com/p/v8/issues/detail?id=2905 |
101 int64_t seed = Time::NowFromSystemTime().ToInternalValue() << 24; | 101 int64_t seed = Time::NowFromSystemTime().ToInternalValue() << 24; |
102 seed ^= TimeTicks::HighResNow().ToInternalValue() << 16; | 102 seed ^= TimeTicks::HighResolutionNow().ToInternalValue() << 16; |
103 seed ^= TimeTicks::Now().ToInternalValue() << 8; | 103 seed ^= TimeTicks::Now().ToInternalValue() << 8; |
104 SetSeed(seed); | 104 SetSeed(seed); |
105 #endif // V8_OS_CYGWIN || V8_OS_WIN | 105 #endif // V8_OS_CYGWIN || V8_OS_WIN |
106 } | 106 } |
107 | 107 |
108 | 108 |
109 int RandomNumberGenerator::NextInt(int max) { | 109 int RandomNumberGenerator::NextInt(int max) { |
110 ASSERT_LE(0, max); | 110 ASSERT_LE(0, max); |
111 | 111 |
112 // Fast path if max is a power of 2. | 112 // Fast path if max is a power of 2. |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 seed_ = seed; | 144 seed_ = seed; |
145 return static_cast<int>(seed >> (48 - bits)); | 145 return static_cast<int>(seed >> (48 - bits)); |
146 } | 146 } |
147 | 147 |
148 | 148 |
149 void RandomNumberGenerator::SetSeed(int64_t seed) { | 149 void RandomNumberGenerator::SetSeed(int64_t seed) { |
150 seed_ = (seed ^ kMultiplier) & kMask; | 150 seed_ = (seed ^ kMultiplier) & kMask; |
151 } | 151 } |
152 | 152 |
153 } } // namespace v8::internal | 153 } } // namespace v8::internal |
OLD | NEW |