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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 fclose(fp); | 75 fclose(fp); |
76 if (n == 1) { | 76 if (n == 1) { |
77 SetSeed(seed); | 77 SetSeed(seed); |
78 return; | 78 return; |
79 } | 79 } |
80 } | 80 } |
81 | 81 |
82 // We cannot assume that random() or rand() were seeded | 82 // We cannot assume that random() or rand() were seeded |
83 // properly, so instead of relying on random() or rand(), | 83 // properly, so instead of relying on random() or rand(), |
84 // we just seed our PRNG using timing data as fallback. | 84 // we just seed our PRNG using timing data as fallback. |
| 85 // This is weak entropy, but it's sufficient, because |
| 86 // it is the responsibility of the embedder to install |
| 87 // an entropy source using v8::V8::SetEntropySource(), |
| 88 // which provides reasonable entropy, see: |
| 89 // https://code.google.com/p/v8/issues/detail?id=2905 |
85 int64_t seed = Time::NowFromSystemTime().ToInternalValue() << 24; | 90 int64_t seed = Time::NowFromSystemTime().ToInternalValue() << 24; |
86 seed ^= TimeTicks::HighResNow().ToInternalValue() << 16; | 91 seed ^= TimeTicks::HighResNow().ToInternalValue() << 16; |
87 seed ^= TimeTicks::Now().ToInternalValue() << 8; | 92 seed ^= TimeTicks::Now().ToInternalValue() << 8; |
88 SetSeed(seed); | 93 SetSeed(seed); |
89 } | 94 } |
90 | 95 |
91 | 96 |
92 int RandomNumberGenerator::NextInt(int max) { | 97 int RandomNumberGenerator::NextInt(int max) { |
93 ASSERT_LE(0, max); | 98 ASSERT_LE(0, max); |
94 | 99 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 seed_ = seed; | 132 seed_ = seed; |
128 return static_cast<int>(seed >> (48 - bits)); | 133 return static_cast<int>(seed >> (48 - bits)); |
129 } | 134 } |
130 | 135 |
131 | 136 |
132 void RandomNumberGenerator::SetSeed(int64_t seed) { | 137 void RandomNumberGenerator::SetSeed(int64_t seed) { |
133 seed_ = (seed ^ kMultiplier) & kMask; | 138 seed_ = (seed ^ kMultiplier) & kMask; |
134 } | 139 } |
135 | 140 |
136 } } // namespace v8::internal | 141 } } // namespace v8::internal |
OLD | NEW |