3 // Copyright (C) 2007, 2008 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 2, 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 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction. Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License. This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
31 /** @file parallel/random_number.h
32 * @brief Random number generator based on the Mersenne twister.
33 * This file is a GNU parallel extension to the Standard C++ Library.
36 // Written by Johannes Singler.
38 #ifndef _GLIBCXX_PARALLEL_RANDOM_NUMBER_H
39 #define _GLIBCXX_PARALLEL_RANDOM_NUMBER_H 1
41 #include <parallel/types.h>
44 namespace __gnu_parallel
46 /** @brief Random number generator, based on the Mersenne twister. */
53 double supremum_reciprocal
;
56 // Assumed to be twice as long as the usual random number.
64 #if _GLIBCXX_SCALE_DOWN_FPU
65 uint64
/*supremum*/, double supremum_reciprocal
)
67 uint64 supremum
, double /*supremum_reciprocal*/)
70 #if _GLIBCXX_SCALE_DOWN_FPU
71 return uint32(x
* supremum_reciprocal
);
73 return static_cast<uint32
>(x
% supremum
);
78 /** @brief Default constructor. Seed with 0. */
80 : mt(0), supremum(0x100000000ULL
),
81 RAND_SUP(1ULL << (sizeof(uint32
) * 8)),
82 supremum_reciprocal(double(supremum
) / double(RAND_SUP
)),
83 RAND_SUP_REC(1.0 / double(RAND_SUP
)),
84 cache(0), bits_left(0) { }
86 /** @brief Constructor.
87 * @param seed Random seed.
88 * @param supremum Generate integer random numbers in the
89 * interval @c [0,supremum). */
90 random_number(uint32 seed
, uint64 supremum
= 0x100000000ULL
)
91 : mt(seed
), supremum(supremum
),
92 RAND_SUP(1ULL << (sizeof(uint32
) * 8)),
93 supremum_reciprocal(double(supremum
) / double(RAND_SUP
)),
94 RAND_SUP_REC(1.0 / double(RAND_SUP
)),
95 cache(0), bits_left(0) { }
97 /** @brief Generate unsigned random 32-bit integer. */
100 { return scale_down(mt(), supremum
, supremum_reciprocal
); }
102 /** @brief Generate unsigned random 32-bit integer in the
103 interval @c [0,local_supremum). */
105 operator()(uint64 local_supremum
)
107 return scale_down(mt(), local_supremum
,
108 double(local_supremum
* RAND_SUP_REC
));
111 /** @brief Generate a number of random bits, run-time parameter.
112 * @param bits Number of bits to generate. */
114 genrand_bits(int bits
)
116 unsigned long res
= cache
& ((1 << bits
) - 1);
117 cache
= cache
>> bits
;
121 cache
|= ((uint64(mt())) << bits_left
);
128 } // namespace __gnu_parallel