tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

rand_util.cc (801B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 // Copyright (c) 2008 The Chromium Authors. All rights reserved.
      4 // Use of this source code is governed by a BSD-style license that can be
      5 // found in the LICENSE file.
      6 
      7 #include "base/rand_util.h"
      8 
      9 #include <math.h>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/logging.h"
     13 #include "mozilla/RandomNum.h"
     14 
     15 namespace base {
     16 
     17 int RandInt(int min, int max) {
     18  DCHECK(min <= max);
     19 
     20  uint64_t range = static_cast<int64_t>(max) - min + 1;
     21  mozilla::Maybe<uint64_t> number = mozilla::RandomUint64();
     22  MOZ_RELEASE_ASSERT(number.isSome());
     23  int result = min + static_cast<int>(number.value() % range);
     24  DCHECK(result >= min && result <= max);
     25  return result;
     26 }
     27 
     28 }  // namespace base