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

Unified Diff: base/rand_util.cc

Issue 6904118: Add a rand_util method for generating a random string. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 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 side-by-side diff with in-line comments
Download patch
Index: base/rand_util.cc
===================================================================
--- base/rand_util.cc (revision 83409)
+++ base/rand_util.cc (working copy)
@@ -41,4 +41,20 @@
return base::RandUint64() % max;
}
+std::string RandString(int length) {
+ const size_t kBitsPerChar = 8;
+ const int kCharsPerInt64 = sizeof(uint64)/sizeof(char);
+
+ std::string result(length, '\0');
+ uint64 entropy = 0;
+ for (size_t i = 0; i < result.size(); ++i) {
+ if (i % kCharsPerInt64 == 0)
+ entropy = RandUint64();
darin (slow to review) 2011/04/29 16:15:36 nit: indentation
+ result[i] = static_cast<char>(entropy);
+ entropy >>= kBitsPerChar;
+ }
+
+ return result;
+}
+
} // namespace base
« base/rand_util.h ('K') | « base/rand_util.h ('k') | base/rand_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698