Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(302)

Side by Side Diff: src/base/utils/random-number-generator.cc

Issue 1541143002: Prevent using 0 as random seed. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "src/base/utils/random-number-generator.h" 5 #include "src/base/utils/random-number-generator.h"
6 6
7 #include <stdio.h> 7 #include <stdio.h>
8 #include <stdlib.h> 8 #include <stdlib.h>
9 9
10 #include <new> 10 #include <new>
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 117
118 int RandomNumberGenerator::Next(int bits) { 118 int RandomNumberGenerator::Next(int bits) {
119 DCHECK_LT(0, bits); 119 DCHECK_LT(0, bits);
120 DCHECK_GE(32, bits); 120 DCHECK_GE(32, bits);
121 XorShift128(&state0_, &state1_); 121 XorShift128(&state0_, &state1_);
122 return static_cast<int>((state0_ + state1_) >> (64 - bits)); 122 return static_cast<int>((state0_ + state1_) >> (64 - bits));
123 } 123 }
124 124
125 125
126 void RandomNumberGenerator::SetSeed(int64_t seed) { 126 void RandomNumberGenerator::SetSeed(int64_t seed) {
127 if (seed == 0) seed = 1;
127 initial_seed_ = seed; 128 initial_seed_ = seed;
128 state0_ = MurmurHash3(bit_cast<uint64_t>(seed)); 129 state0_ = MurmurHash3(bit_cast<uint64_t>(seed));
129 state1_ = MurmurHash3(state0_); 130 state1_ = MurmurHash3(state0_);
130 } 131 }
131 132
132 133
133 uint64_t RandomNumberGenerator::MurmurHash3(uint64_t h) { 134 uint64_t RandomNumberGenerator::MurmurHash3(uint64_t h) {
134 h ^= h >> 33; 135 h ^= h >> 33;
135 h *= V8_UINT64_C(0xFF51AFD7ED558CCD); 136 h *= V8_UINT64_C(0xFF51AFD7ED558CCD);
136 h ^= h >> 33; 137 h ^= h >> 33;
137 h *= V8_UINT64_C(0xC4CEB9FE1A85EC53); 138 h *= V8_UINT64_C(0xC4CEB9FE1A85EC53);
138 h ^= h >> 33; 139 h ^= h >> 33;
139 return h; 140 return h;
140 } 141 }
141 142
142 } // namespace base 143 } // namespace base
143 } // namespace v8 144 } // namespace v8
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698