3 // Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
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
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.
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.
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/>.
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.
30 // Written by Johannes Singler.
32 #ifndef _GLIBCXX_PARALLEL_RANDOM_NUMBER_H
33 #define _GLIBCXX_PARALLEL_RANDOM_NUMBER_H 1
35 #include <parallel/types.h>
38 namespace __gnu_parallel
40 /** @brief Random number generator, based on the Mersenne twister. */
47 double supremum_reciprocal;
50 // Assumed to be twice as long as the usual random number.
58 #if _GLIBCXX_SCALE_DOWN_FPU
59 uint64 /*supremum*/, double supremum_reciprocal)
61 uint64 supremum, double /*supremum_reciprocal*/)
64 #if _GLIBCXX_SCALE_DOWN_FPU
65 return uint32(x * supremum_reciprocal);
67 return static_cast<uint32>(x % supremum);
72 /** @brief Default constructor. Seed with 0. */
74 : mt(0), supremum(0x100000000ULL),
75 RAND_SUP(1ULL << (sizeof(uint32) * 8)),
76 supremum_reciprocal(double(supremum) / double(RAND_SUP)),
77 RAND_SUP_REC(1.0 / double(RAND_SUP)),
78 cache(0), bits_left(0) { }
80 /** @brief Constructor.
81 * @param seed Random seed.
82 * @param supremum Generate integer random numbers in the
83 * interval @c [0,supremum). */
84 random_number(uint32 seed, uint64 supremum = 0x100000000ULL)
85 : mt(seed), supremum(supremum),
86 RAND_SUP(1ULL << (sizeof(uint32) * 8)),
87 supremum_reciprocal(double(supremum) / double(RAND_SUP)),
88 RAND_SUP_REC(1.0 / double(RAND_SUP)),
89 cache(0), bits_left(0) { }
91 /** @brief Generate unsigned random 32-bit integer. */
94 { return scale_down(mt(), supremum, supremum_reciprocal); }
96 /** @brief Generate unsigned random 32-bit integer in the
97 interval @c [0,local_supremum). */
99 operator()(uint64 local_supremum)
101 return scale_down(mt(), local_supremum,
102 double(local_supremum * RAND_SUP_REC));
105 /** @brief Generate a number of random bits, run-time parameter.
106 * @param bits Number of bits to generate. */
108 genrand_bits(int bits)
110 unsigned long res = cache & ((1 << bits) - 1);
111 cache = cache >> bits;
115 cache |= ((uint64(mt())) << bits_left);
122 } // namespace __gnu_parallel
124 #endif /* _GLIBCXX_PARALLEL_RANDOM_NUMBER_H */