1
/* boost random/uniform_01.hpp header file
3
* Copyright Jens Maurer 2000-2001
4
* Permission to use, copy, modify, sell, and distribute this software
5
* is hereby granted without fee provided that the above copyright notice
6
* appears in all copies and that both that copyright notice and this
7
* permission notice appear in supporting documentation,
9
* Jens Maurer makes no representations about the suitability of this
10
* software for any purpose. It is provided "as is" without express or
13
* See http://www.boost.org for most recent version including documentation.
15
* $Id: uniform_01.hpp,v 1.1 2004/02/27 03:16:46 pseudonym Exp $
18
* 2001-02-18 moved to individual header files
21
#ifndef BOOST_RANDOM_UNIFORM_01_HPP
22
#define BOOST_RANDOM_UNIFORM_01_HPP
24
#include <boost/config.hpp>
25
#include <boost/limits.hpp>
26
#include <boost/static_assert.hpp>
30
// Because it is so commonly used: uniform distribution on the real [0..1)
31
// range. This allows for specializations to avoid a costly int -> float
32
// conversion plus float multiplication
33
template<class UniformRandomNumberGenerator, class RealType = double>
37
typedef uniform_01<UniformRandomNumberGenerator, RealType> adaptor_type;
38
typedef UniformRandomNumberGenerator base_type;
39
typedef RealType result_type;
41
BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
43
#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
44
BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
47
explicit uniform_01(base_type & rng)
49
_factor(result_type(1) /
50
(result_type(_rng->max()-_rng->min()) +
51
result_type(std::numeric_limits<base_result>::is_integer ? 1 : 0)))
54
// compiler-generated copy ctor and copy assignment are fine
56
result_type min() const { return result_type(0); }
57
result_type max() const { return result_type(1); }
58
adaptor_type& adaptor() { return *this; }
59
base_type& base() const { return *_rng; }
62
result_type operator()() {
63
return result_type((*_rng)() - _rng->min()) * _factor;
66
#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
67
friend bool operator==(const uniform_01& x, const uniform_01& y)
68
{ return *x._rng == *y._rng; }
70
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
71
template<class CharT, class Traits>
72
friend std::basic_ostream<CharT,Traits>&
73
operator<<(std::basic_ostream<CharT,Traits>& os, const uniform_01&)
78
template<class CharT, class Traits>
79
friend std::basic_istream<CharT,Traits>&
80
operator>>(std::basic_istream<CharT,Traits>& is, uniform_01&)
87
// Use a member function
88
bool operator==(const uniform_01& rhs) const
89
{ return *_rng == *rhs._rng; }
92
typedef typename base_type::result_type base_result;
97
#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
98
// A definition is required even for integral static constants
99
template<class UniformRandomNumberGenerator, class RealType>
100
const bool uniform_01<UniformRandomNumberGenerator, RealType>::has_fixed_range;
105
#endif // BOOST_RANDOM_UNIFORM_01_HPP