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

Side by Side Diff: chrome/common/rand_util.cc

Issue 4079: Porting refactoring changes:... (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 12 years, 2 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
OLDNEW
(Empty)
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/common/rand_util.h"
6
7 #include <stdlib.h>
8
9 #include "base/logging.h"
10
11 namespace rand_util {
12
13 int RandInt(int min, int max) {
14 return RandIntSecure(min, max);
15 }
16
17 int RandIntSecure(int min, int max) {
18 unsigned int number;
19 // This code will not work on win2k, which we do not support.
20 errno_t rv = rand_s(&number);
21 DCHECK(rv == 0) << "rand_s failed with error " << rv;
22
23 // From the rand man page, use this instead of just rand() % max, so that the
24 // higher bits are used.
25 return min + static_cast<int>(static_cast<double>(max - min + 1.0) *
26 (number / (UINT_MAX + 1.0)));
27 }
28
29 } // namespace rand_util
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698