libstdc++
random_number.h
Go to the documentation of this file.
1// -*- C++ -*-
2
3// Copyright (C) 2007-2025 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the terms
7// of the GNU General Public License as published by the Free Software
8// Foundation; either version 3, or (at your option) any later
9// version.
10
11// This library is distributed in the hope that it will be useful, but
12// WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file parallel/random_number.h
26 * @brief Random number generator based on the Mersenne twister.
27 * This file is a GNU parallel extension to the Standard C++ Library.
28 */
29
30// Written by Johannes Singler.
31
32#ifndef _GLIBCXX_PARALLEL_RANDOM_NUMBER_H
33#define _GLIBCXX_PARALLEL_RANDOM_NUMBER_H 1
34
35#include <parallel/types.h>
36#include <tr1/random>
37#include <limits>
38
39namespace __gnu_parallel
40{
41#pragma GCC diagnostic push
42#pragma GCC diagnostic ignored "-Wlong-long" // LL literal
43
44 /** @brief Random number generator, based on the Mersenne twister. */
46 {
47 private:
48 std::tr1::mt19937 _M_mt;
49 uint64_t _M_supremum;
50 uint64_t _M_rand_sup;
51 double _M_supremum_reciprocal;
52 double _M_rand_sup_reciprocal;
53
54 // Assumed to be twice as long as the usual random number.
55 uint64_t __cache;
56
57 // Bit results.
58 int __bits_left;
59
60 static uint32_t
61 __scale_down(uint64_t __x,
63 uint64_t /*_M_supremum*/, double _M_supremum_reciprocal)
64#else
65 uint64_t _M_supremum, double /*_M_supremum_reciprocal*/)
66#endif
67 {
68#if _GLIBCXX_SCALE_DOWN_FPU
69 return uint32_t(__x * _M_supremum_reciprocal);
70#else
71 return static_cast<uint32_t>(__x % _M_supremum);
72#endif
73 }
74
75 public:
76 /** @brief Default constructor. Seed with 0. */
78 : _M_mt(0), _M_supremum(0x100000000ULL),
79 _M_rand_sup(1ULL << std::numeric_limits<uint32_t>::digits),
80 _M_supremum_reciprocal(double(_M_supremum) / double(_M_rand_sup)),
81 _M_rand_sup_reciprocal(1.0 / double(_M_rand_sup)),
82 __cache(0), __bits_left(0) { }
83
84 /** @brief Constructor.
85 * @param __seed Random __seed.
86 * @param _M_supremum Generate integer random numbers in the
87 * interval @c [0,_M_supremum). */
88 _RandomNumber(uint32_t __seed, uint64_t _M_supremum = 0x100000000ULL)
89 : _M_mt(__seed), _M_supremum(_M_supremum),
90 _M_rand_sup(1ULL << std::numeric_limits<uint32_t>::digits),
91 _M_supremum_reciprocal(double(_M_supremum) / double(_M_rand_sup)),
92 _M_rand_sup_reciprocal(1.0 / double(_M_rand_sup)),
93 __cache(0), __bits_left(0) { }
94
95 /** @brief Generate unsigned random 32-bit integer. */
96 uint32_t
98 { return __scale_down(_M_mt(), _M_supremum, _M_supremum_reciprocal); }
99
100 /** @brief Generate unsigned random 32-bit integer in the
101 interval @c [0,local_supremum). */
102 uint32_t
103 operator()(uint64_t local_supremum)
104 {
105 return __scale_down(_M_mt(), local_supremum,
106 double(local_supremum * _M_rand_sup_reciprocal));
107 }
108
109 /** @brief Generate a number of random bits, run-time parameter.
110 * @param __bits Number of bits to generate. */
111 unsigned long
112 __genrand_bits(int __bits)
113 {
114 unsigned long __res = __cache & ((1 << __bits) - 1);
115 __cache = __cache >> __bits;
116 __bits_left -= __bits;
117 if (__bits_left < 32)
118 {
119 __cache |= ((uint64_t(_M_mt())) << __bits_left);
120 __bits_left += 32;
121 }
122 return __res;
123 }
124};
125
126#pragma GCC diagnostic pop
127
128} // namespace __gnu_parallel
129
130#endif /* _GLIBCXX_PARALLEL_RANDOM_NUMBER_H */
#define _GLIBCXX_SCALE_DOWN_FPU
Use floating-point scaling instead of modulo for mapping random numbers to a range....
Basic types and typedefs. This file is a GNU parallel extension to the Standard C++ Library.
ISO C++ entities toplevel namespace is std.
GNU parallel code for public use.
unsigned long __genrand_bits(int __bits)
Generate a number of random bits, run-time parameter.
uint32_t operator()(uint64_t local_supremum)
Generate unsigned random 32-bit integer in the interval [0,local_supremum).
uint32_t operator()()
Generate unsigned random 32-bit integer.
_RandomNumber()
Default constructor. Seed with 0.
_RandomNumber(uint32_t __seed, uint64_t _M_supremum=0x100000000ULL)
Constructor.