pstore2
random.hpp
Go to the documentation of this file.
1 //===- include/pstore/support/random.hpp ------------------*- mode: C++ -*-===//
2 //* _ *
3 //* _ __ __ _ _ __ __| | ___ _ __ ___ *
4 //* | '__/ _` | '_ \ / _` |/ _ \| '_ ` _ \ *
5 //* | | | (_| | | | | (_| | (_) | | | | | | *
6 //* |_| \__,_|_| |_|\__,_|\___/|_| |_| |_| *
7 //* *
8 //===----------------------------------------------------------------------===//
9 //
10 // Part of the pstore project, under the Apache License v2.0 with LLVM Exceptions.
11 // See https://github.com/SNSystems/pstore/blob/master/LICENSE.txt for license
12 // information.
13 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 //
15 //===----------------------------------------------------------------------===//
17 
18 #ifndef PSTORE_SUPPORT_RANDOM_HPP
19 #define PSTORE_SUPPORT_RANDOM_HPP
20 
21 #include <limits>
22 #include <random>
23 
24 namespace pstore {
25 
26  template <typename Ty>
28  public:
30  : generator_ (device_ ()) {}
31 
32  Ty get (Ty max) { return distribution_ (generator_) % max; }
33  Ty get () {
34  auto const max = std::numeric_limits<Ty>::max ();
35  static_assert (max > Ty (0), "max must be > 0");
36  return get (max);
37  }
38 
39  private:
40  std::random_device device_;
41  std::mt19937_64 generator_;
42  std::uniform_int_distribution<Ty> distribution_;
43  };
44 
45 } // namespace pstore
46 
47 #endif // PSTORE_SUPPORT_RANDOM_HPP
Definition: random.hpp:27
Definition: nonpod2.cpp:40