2
0
mirror of https://github.com/boostorg/random.git synced 2026-01-19 04:22:17 +00:00

This commit was manufactured by cvs2svn to create tag

'merged_to_RC_1_34_0'.

[SVN r37938]
This commit is contained in:
nobody
2007-06-07 20:53:46 +00:00
parent 0efede690c
commit 3701a00764
8 changed files with 123 additions and 210 deletions

View File

@@ -65,8 +65,8 @@ public:
void seed(typename MLCG1::result_type seed1,
typename MLCG2::result_type seed2)
{
_mlcg1.seed(seed1);
_mlcg2.seed(seed2);
_mlcg1(seed1);
_mlcg2(seed2);
}
template<class It> void seed(It& first, It last)

View File

@@ -43,10 +43,10 @@ namespace detail {
template<class IntType>
static IntType add(IntType m, IntType x, IntType c)
{
if (x < m - c)
return x + c;
else
return x - (m-c);
x += (c-m);
if(x < 0)
x += m;
return x;
}
};

View File

@@ -0,0 +1,91 @@
/* boost random/detail/signed_unsigned_compare.hpp header file
*
* Copyright Jens Maurer 2000-2001
* Distributed under the Boost Software License, Version 1.0. (See
* accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*
* See http://www.boost.org for most recent version including documentation.
*
* Revision history
*/
#ifndef BOOST_RANDOM_DETAIL_SIGNED_UNSIGNED_COMPARE
#define BOOST_RANDOM_DETAIL_SIGNED_UNSIGNED_COMPARE
#include <boost/limits.hpp>
namespace boost {
namespace random {
/*
* Correctly compare two numbers whose types possibly differ in signedness.
* See boost::numeric_cast<> for the general idea.
* Most "if" statements involve only compile-time constants, so the
* optimizing compiler can do its job easily.
*
* With most compilers, the straightforward implementation produces a
* bunch of (legitimate) warnings. Some template magic helps, though.
*/
namespace detail {
template<bool signed1, bool signed2>
struct do_compare
{ };
template<>
struct do_compare<false, false>
{
// cast to the larger type is automatic with built-in types
template<class T1, class T2>
static bool equal(T1 x, T2 y) { return x == y; }
template<class T1, class T2>
static bool lessthan(T1 x, T2 y) { return x < y; }
};
template<>
struct do_compare<true, true> : do_compare<false, false>
{ };
template<>
struct do_compare<true, false>
{
template<class T1, class T2>
static bool equal(T1 x, T2 y) { return x >= 0 && static_cast<T2>(x) == y; }
template<class T1, class T2>
static bool lessthan(T1 x, T2 y) { return x < 0 || static_cast<T2>(x) < y; }
};
template<>
struct do_compare<false, true>
{
template<class T1, class T2>
static bool equal(T1 x, T2 y) { return y >= 0 && x == static_cast<T1>(y); }
template<class T1, class T2>
static bool lessthan(T1 x, T2 y) { return y >= 0 && x < static_cast<T1>(y); }
};
} // namespace detail
template<class T1, class T2>
int equal_signed_unsigned(T1 x, T2 y)
{
typedef std::numeric_limits<T1> x_traits;
typedef std::numeric_limits<T2> y_traits;
return detail::do_compare<x_traits::is_signed, y_traits::is_signed>::equal(x, y);
}
template<class T1, class T2>
int lessthan_signed_unsigned(T1 x, T2 y)
{
typedef std::numeric_limits<T1> x_traits;
typedef std::numeric_limits<T2> y_traits;
return detail::do_compare<x_traits::is_signed, y_traits::is_signed>::lessthan(x, y);
}
} // namespace random
} // namespace boost
#endif // BOOST_RANDOM_DETAIL_SIGNED_UNSIGNED_COMPARE

View File

@@ -1,164 +0,0 @@
/* boost random/detail/signed_unsigned_tools.hpp header file
*
* Copyright Jens Maurer 2006
* Distributed under the Boost Software License, Version 1.0. (See
* accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*
* See http://www.boost.org for most recent version including documentation.
*/
#ifndef BOOST_RANDOM_DETAIL_SIGNED_UNSIGNED_TOOLS
#define BOOST_RANDOM_DETAIL_SIGNED_UNSIGNED_TOOLS
#include <boost/limits.hpp>
#include <boost/config.hpp>
namespace boost {
namespace random {
namespace detail {
/*
* Given an (integral) type T, returns the type "unsigned T".
* (type_traits appears to be lacking the feature)
*/
template<class T>
struct make_unsigned { };
template<>
struct make_unsigned<char>
{
typedef unsigned char type;
};
template<>
struct make_unsigned<signed char>
{
typedef unsigned char type;
};
template<>
struct make_unsigned<unsigned char>
{
typedef unsigned char type;
};
template<>
struct make_unsigned<short>
{
typedef unsigned short type;
};
template<>
struct make_unsigned<unsigned short>
{
typedef unsigned short type;
};
template<>
struct make_unsigned<int>
{
typedef unsigned int type;
};
template<>
struct make_unsigned<unsigned int>
{
typedef unsigned int type;
};
template<>
struct make_unsigned<long>
{
typedef unsigned long type;
};
template<>
struct make_unsigned<unsigned long>
{
typedef unsigned long type;
};
#ifdef BOOST_HAS_LONG_LONG
template<>
struct make_unsigned<long long>
{
typedef unsigned long long type;
};
template<>
struct make_unsigned<unsigned long long>
{
typedef unsigned long long type;
};
#endif
/*
* Compute x - y, we know that x >= y, return an unsigned value.
*/
template<class T, bool sgn = std::numeric_limits<T>::is_signed>
struct subtract { };
template<class T>
struct subtract<T, /* signed */ false>
{
typedef T result_type;
result_type operator()(T x, T y) { return x - y; }
};
template<class T>
struct subtract<T, /* signed */ true>
{
typedef typename make_unsigned<T>::type result_type;
result_type operator()(T x, T y)
{
if (y >= 0) // because x >= y, it follows that x >= 0, too
return result_type(x) - result_type(y);
if (x >= 0) // y < 0
// avoid the nasty two's complement case for y == min()
return result_type(x) + result_type(-(y+1)) + 1;
// both x and y are negative: no signed overflow
return result_type(x - y);
}
};
/*
* Compute x + y, x is unsigned, result fits in type of "y".
*/
template<class T1, class T2, bool sgn = std::numeric_limits<T2>::is_signed>
struct add { };
template<class T1, class T2>
struct add<T1, T2, /* signed */ false>
{
typedef T2 result_type;
result_type operator()(T1 x, T2 y) { return x + y; }
};
template<class T1, class T2>
struct add<T1, T2, /* signed */ true>
{
typedef T2 result_type;
result_type operator()(T1 x, T2 y)
{
if (y >= 0)
return x + y;
// y < 0
if (x >= T1(-(y+1))) // result >= 0 after subtraction
// avoid the nasty two's complement edge case for y == min()
return T2(x - T1(-(y+1)) - 1);
// abs(x) < abs(y), thus T2 able to represent x
return T2(x) + y;
}
};
} // namespace detail
} // namespace random
} // namespace boost
#endif // BOOST_RANDOM_DETAIL_SIGNED_UNSIGNED_TOOLS

View File

@@ -18,7 +18,6 @@
#include <iostream>
#include <cassert>
#include <stdexcept>
#include <boost/config.hpp>
#include <boost/static_assert.hpp>
#include <boost/random/detail/const_mod.hpp>

View File

@@ -60,8 +60,8 @@ public:
// compiler-generated copy ctor and assignment operator are fine
RealType mean() const { return _mean; }
RealType sigma() const { return _sigma; }
RealType& mean() const { return _mean; }
RealType& sigma() const { return _sigma; }
void reset() { _normal.reset(); }
template<class Engine>

View File

@@ -55,11 +55,7 @@ public:
void reset() { }
result_type operator()() {
for (;;) {
result_type result = result_type(_rng() - (_rng.min)()) * _factor;
if (result < result_type(1))
return result;
}
return result_type(_rng() - (_rng.min)()) * _factor;
}
#if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)

View File

@@ -24,7 +24,7 @@
#include <boost/static_assert.hpp>
#include <boost/detail/workaround.hpp>
#include <boost/random/uniform_smallint.hpp>
#include <boost/random/detail/signed_unsigned_tools.hpp>
#include <boost/random/detail/signed_unsigned_compare.hpp>
#ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
#include <boost/type_traits/is_float.hpp>
#endif
@@ -38,7 +38,6 @@ class uniform_int
public:
typedef IntType input_type;
typedef IntType result_type;
typedef typename random::detail::make_unsigned<result_type>::type range_type;
explicit uniform_int(IntType min = 0, IntType max = 9)
: _min(min), _max(max)
@@ -60,47 +59,43 @@ public:
result_type operator()(Engine& eng)
{
typedef typename Engine::result_type base_result;
// ranges are always unsigned
typedef typename random::detail::make_unsigned<base_result>::type base_unsigned;
const base_result bmin = (eng.min)();
const base_unsigned brange =
random::detail::subtract<base_result>()((eng.max)(), (eng.min)());
base_result bmin = (eng.min)();
base_result brange = (eng.max)() - (eng.min)();
if(_range == 0) {
return _min;
} else if(brange == _range) {
} else if(random::equal_signed_unsigned(brange, _range)) {
// this will probably never happen in real life
// basically nothing to do; just take care we don't overflow / underflow
base_unsigned v = random::detail::subtract<base_result>()(eng(), bmin);
return random::detail::add<base_unsigned, result_type>()(v, _min);
} else if(brange < _range) {
return static_cast<result_type>(eng() - bmin) + _min;
} else if(random::lessthan_signed_unsigned(brange, _range)) {
// use rejection method to handle things like 0..3 --> 0..4
for(;;) {
// concatenate several invocations of the base RNG
// take extra care to avoid overflows
range_type limit;
if(_range == (std::numeric_limits<range_type>::max)()) {
limit = _range/(range_type(brange)+1);
if(_range % range_type(brange)+1 == range_type(brange))
result_type limit;
if(_range == (std::numeric_limits<result_type>::max)()) {
limit = _range/(result_type(brange)+1);
if(_range % result_type(brange)+1 == result_type(brange))
++limit;
} else {
limit = (_range+1)/(range_type(brange)+1);
limit = (_range+1)/(result_type(brange)+1);
}
// We consider "result" as expressed to base (brange+1):
// For every power of (brange+1), we determine a random factor
range_type result = range_type(0);
range_type mult = range_type(1);
result_type result = result_type(0);
result_type mult = result_type(1);
while(mult <= limit) {
result += random::detail::subtract<base_result>()(eng(), bmin) * mult;
mult *= range_type(brange)+range_type(1);
result += (eng() - bmin) * mult;
mult *= result_type(brange)+result_type(1);
}
if(mult == limit)
// _range+1 is an integer power of brange+1: no rejections required
return result;
// _range/mult < brange+1 -> no endless loop
result += uniform_int<range_type>(0, _range/mult)(eng) * mult;
result += uniform_int<result_type>(0, _range/mult)(eng) * mult;
if(result <= _range)
return random::detail::add<range_type, result_type>()(result, _min);
return result + _min;
}
} else { // brange > range
if(brange / _range > 4 /* quantization_cutoff */ ) {
@@ -110,12 +105,11 @@ public:
} else {
// use rejection method to handle cases like 0..5 -> 0..4
for(;;) {
base_unsigned result =
random::detail::subtract<base_result>()(eng(), bmin);
base_result result = eng() - bmin;
// result and range are non-negative, and result is possibly larger
// than range, so the cast is safe
if(result <= static_cast<base_unsigned>(_range))
return random::detail::add<base_unsigned, result_type>()(result, _min);
if(result <= static_cast<base_result>(_range))
return result + _min;
}
}
}
@@ -147,13 +141,10 @@ public:
private:
void init()
{
_range = random::detail::subtract<result_type>()(_max, _min);
_range = _max - _min;
}
// The result_type may be signed or unsigned, but the _range is always
// unsigned.
result_type _min, _max;
range_type _range;
result_type _min, _max, _range;
};
} // namespace boost