OLD | NEW |
| (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 | |
OLD | NEW |