2007-09-12 00:32:51 +02:00
|
|
|
// -*- C++ -*-
|
|
|
|
|
2009-04-09 17:00:19 +02:00
|
|
|
// Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
|
2007-09-12 00:32:51 +02:00
|
|
|
//
|
|
|
|
// This file is part of the GNU ISO C++ Library. This library is free
|
|
|
|
// software; you can redistribute it and/or modify it under the terms
|
|
|
|
// of the GNU General Public License as published by the Free Software
|
2009-04-09 17:00:19 +02:00
|
|
|
// Foundation; either version 3, or (at your option) any later
|
2007-09-12 00:32:51 +02:00
|
|
|
// version.
|
|
|
|
|
|
|
|
// This library is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
|
2009-04-09 17:00:19 +02:00
|
|
|
// Under Section 7 of GPL version 3, you are granted additional
|
|
|
|
// permissions described in the GCC Runtime Library Exception, version
|
|
|
|
// 3.1, as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License and
|
|
|
|
// a copy of the GCC Runtime Library Exception along with this program;
|
|
|
|
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
|
|
|
|
// <http://www.gnu.org/licenses/>.
|
2007-09-12 00:32:51 +02:00
|
|
|
|
|
|
|
/** @file parallel/random_number.h
|
|
|
|
* @brief Random number generator based on the Mersenne twister.
|
|
|
|
* This file is a GNU parallel extension to the Standard C++ Library.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Written by Johannes Singler.
|
|
|
|
|
|
|
|
#ifndef _GLIBCXX_PARALLEL_RANDOM_NUMBER_H
|
|
|
|
#define _GLIBCXX_PARALLEL_RANDOM_NUMBER_H 1
|
|
|
|
|
|
|
|
#include <parallel/types.h>
|
2008-02-17 00:21:20 +01:00
|
|
|
#include <tr1/random>
|
2009-10-02 09:47:48 +02:00
|
|
|
#include <limits>
|
2007-09-12 00:32:51 +02:00
|
|
|
|
|
|
|
namespace __gnu_parallel
|
|
|
|
{
|
|
|
|
/** @brief Random number generator, based on the Mersenne twister. */
|
2009-09-16 11:47:25 +02:00
|
|
|
class _RandomNumber
|
2007-09-12 00:32:51 +02:00
|
|
|
{
|
|
|
|
private:
|
2009-09-17 10:42:39 +02:00
|
|
|
std::tr1::mt19937 _M_mt;
|
2009-10-02 09:47:48 +02:00
|
|
|
uint64_t _M_supremum;
|
|
|
|
uint64_t _M_rand_sup;
|
2009-09-17 10:42:39 +02:00
|
|
|
double _M_supremum_reciprocal;
|
|
|
|
double _M_rand_sup_reciprocal;
|
2008-02-17 00:21:20 +01:00
|
|
|
|
|
|
|
// Assumed to be twice as long as the usual random number.
|
2009-10-02 09:47:48 +02:00
|
|
|
uint64_t __cache;
|
2008-02-17 00:21:20 +01:00
|
|
|
|
|
|
|
// Bit results.
|
2009-09-16 11:47:25 +02:00
|
|
|
int __bits_left;
|
2008-02-17 00:21:20 +01:00
|
|
|
|
2009-10-02 09:47:48 +02:00
|
|
|
static uint32_t
|
|
|
|
__scale_down(uint64_t __x,
|
2007-09-12 00:32:51 +02:00
|
|
|
#if _GLIBCXX_SCALE_DOWN_FPU
|
2009-10-02 09:47:48 +02:00
|
|
|
uint64_t /*_M_supremum*/, double _M_supremum_reciprocal)
|
2007-09-12 00:32:51 +02:00
|
|
|
#else
|
2009-10-02 09:47:48 +02:00
|
|
|
uint64_t _M_supremum, double /*_M_supremum_reciprocal*/)
|
2007-09-12 00:32:51 +02:00
|
|
|
#endif
|
2009-09-17 10:42:39 +02:00
|
|
|
{
|
2007-09-12 00:32:51 +02:00
|
|
|
#if _GLIBCXX_SCALE_DOWN_FPU
|
2009-10-02 09:47:48 +02:00
|
|
|
return uint32_t(__x * _M_supremum_reciprocal);
|
2007-09-12 00:32:51 +02:00
|
|
|
#else
|
2009-10-02 09:47:48 +02:00
|
|
|
return static_cast<uint32_t>(__x % _M_supremum);
|
2007-09-12 00:32:51 +02:00
|
|
|
#endif
|
2009-09-17 10:42:39 +02:00
|
|
|
}
|
2007-09-12 00:32:51 +02:00
|
|
|
|
2008-01-10 03:07:41 +01:00
|
|
|
public:
|
|
|
|
/** @brief Default constructor. Seed with 0. */
|
2009-09-16 11:47:25 +02:00
|
|
|
_RandomNumber()
|
|
|
|
: _M_mt(0), _M_supremum(0x100000000ULL),
|
2009-10-02 09:47:48 +02:00
|
|
|
_M_rand_sup(1ULL << std::numeric_limits<uint32_t>::digits),
|
2009-09-16 15:50:17 +02:00
|
|
|
_M_supremum_reciprocal(double(_M_supremum) / double(_M_rand_sup)),
|
|
|
|
_M_rand_sup_reciprocal(1.0 / double(_M_rand_sup)),
|
2009-09-16 11:47:25 +02:00
|
|
|
__cache(0), __bits_left(0) { }
|
2008-01-10 03:07:41 +01:00
|
|
|
|
|
|
|
/** @brief Constructor.
|
2009-09-16 11:47:25 +02:00
|
|
|
* @param __seed Random __seed.
|
|
|
|
* @param _M_supremum Generate integer random numbers in the
|
2009-11-06 23:15:17 +01:00
|
|
|
* interval @c [0,_M_supremum). */
|
2009-10-02 09:47:48 +02:00
|
|
|
_RandomNumber(uint32_t __seed, uint64_t _M_supremum = 0x100000000ULL)
|
2009-09-16 11:47:25 +02:00
|
|
|
: _M_mt(__seed), _M_supremum(_M_supremum),
|
2009-10-02 09:47:48 +02:00
|
|
|
_M_rand_sup(1ULL << std::numeric_limits<uint32_t>::digits),
|
2009-09-16 15:50:17 +02:00
|
|
|
_M_supremum_reciprocal(double(_M_supremum) / double(_M_rand_sup)),
|
|
|
|
_M_rand_sup_reciprocal(1.0 / double(_M_rand_sup)),
|
2009-09-16 11:47:25 +02:00
|
|
|
__cache(0), __bits_left(0) { }
|
2008-01-10 03:07:41 +01:00
|
|
|
|
|
|
|
/** @brief Generate unsigned random 32-bit integer. */
|
2009-10-02 09:47:48 +02:00
|
|
|
uint32_t
|
2008-01-10 03:07:41 +01:00
|
|
|
operator()()
|
2009-09-16 11:47:25 +02:00
|
|
|
{ return __scale_down(_M_mt(), _M_supremum, _M_supremum_reciprocal); }
|
2008-01-10 03:07:41 +01:00
|
|
|
|
|
|
|
/** @brief Generate unsigned random 32-bit integer in the
|
2009-11-06 23:15:17 +01:00
|
|
|
interval @c [0,local_supremum). */
|
2009-10-02 09:47:48 +02:00
|
|
|
uint32_t
|
|
|
|
operator()(uint64_t local_supremum)
|
2008-01-10 03:07:41 +01:00
|
|
|
{
|
2009-09-16 11:47:25 +02:00
|
|
|
return __scale_down(_M_mt(), local_supremum,
|
2009-09-17 10:42:39 +02:00
|
|
|
double(local_supremum * _M_rand_sup_reciprocal));
|
2008-01-10 03:07:41 +01:00
|
|
|
}
|
2007-09-12 00:32:51 +02:00
|
|
|
|
2008-01-10 03:07:41 +01:00
|
|
|
/** @brief Generate a number of random bits, run-time parameter.
|
|
|
|
* @param bits Number of bits to generate. */
|
|
|
|
unsigned long
|
2009-09-16 15:50:17 +02:00
|
|
|
__genrand_bits(int __bits)
|
2008-01-10 03:07:41 +01:00
|
|
|
{
|
2009-09-16 15:50:17 +02:00
|
|
|
unsigned long __res = __cache & ((1 << __bits) - 1);
|
|
|
|
__cache = __cache >> __bits;
|
|
|
|
__bits_left -= __bits;
|
2009-09-16 11:47:25 +02:00
|
|
|
if (__bits_left < 32)
|
2009-09-17 10:42:39 +02:00
|
|
|
{
|
2009-10-02 09:47:48 +02:00
|
|
|
__cache |= ((uint64_t(_M_mt())) << __bits_left);
|
2009-09-17 10:42:39 +02:00
|
|
|
__bits_left += 32;
|
|
|
|
}
|
2009-09-16 11:47:25 +02:00
|
|
|
return __res;
|
2008-01-10 03:07:41 +01:00
|
|
|
}
|
2007-09-12 00:32:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace __gnu_parallel
|
|
|
|
|
2008-07-15 12:06:58 +02:00
|
|
|
#endif /* _GLIBCXX_PARALLEL_RANDOM_NUMBER_H */
|