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

Side by Side Diff: components/variations/entropy_provider.cc

Issue 222373002: Remove use of random_shuffle() in PermutedEntropyProvider. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 6 years, 8 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 | Annotate | Revision Log
« 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium 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 "components/variations/entropy_provider.h" 5 #include "components/variations/entropy_provider.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <limits> 8 #include <limits>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 43
44 return value % range; 44 return value % range;
45 } 45 }
46 46
47 void PermuteMappingUsingRandomizationSeed(uint32 randomization_seed, 47 void PermuteMappingUsingRandomizationSeed(uint32 randomization_seed,
48 std::vector<uint16>* mapping) { 48 std::vector<uint16>* mapping) {
49 for (size_t i = 0; i < mapping->size(); ++i) 49 for (size_t i = 0; i < mapping->size(); ++i)
50 (*mapping)[i] = static_cast<uint16>(i); 50 (*mapping)[i] = static_cast<uint16>(i);
51 51
52 SeededRandGenerator generator(randomization_seed); 52 SeededRandGenerator generator(randomization_seed);
53 std::random_shuffle(mapping->begin(), mapping->end(), generator); 53
54 // Do a deterministic random shuffle of the mapping using |generator|.
55 //
56 // Note: This logic is identical to the following call with libstdc++ and VS:
57 //
58 // std::random_shuffle(mapping->begin(), mapping->end(), generator);
59 //
60 // However, this is not guaranteed by the spec and some implementations (e.g.
61 // libc++) use a different algorithm. To ensure results are consistent
62 // regardless of the compiler toolchain used, use our own version.
63 for (size_t i = 1; i < mapping->size(); ++i) {
64 // Pick an element in mapping[:i+1] with which to exchange mapping[i].
65 size_t j = generator(i + 1);
66 std::swap((*mapping)[i], (*mapping)[j]);
67 }
54 } 68 }
55 69
56 } // namespace internal 70 } // namespace internal
57 71
58 SHA1EntropyProvider::SHA1EntropyProvider(const std::string& entropy_source) 72 SHA1EntropyProvider::SHA1EntropyProvider(const std::string& entropy_source)
59 : entropy_source_(entropy_source) { 73 : entropy_source_(entropy_source) {
60 } 74 }
61 75
62 SHA1EntropyProvider::~SHA1EntropyProvider() { 76 SHA1EntropyProvider::~SHA1EntropyProvider() {
63 } 77 }
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 } 124 }
111 125
112 uint16 PermutedEntropyProvider::GetPermutedValue( 126 uint16 PermutedEntropyProvider::GetPermutedValue(
113 uint32 randomization_seed) const { 127 uint32 randomization_seed) const {
114 std::vector<uint16> mapping(low_entropy_source_max_); 128 std::vector<uint16> mapping(low_entropy_source_max_);
115 internal::PermuteMappingUsingRandomizationSeed(randomization_seed, &mapping); 129 internal::PermuteMappingUsingRandomizationSeed(randomization_seed, &mapping);
116 return mapping[low_entropy_source_]; 130 return mapping[low_entropy_source_];
117 } 131 }
118 132
119 } // namespace metrics 133 } // namespace metrics
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