source: NonGTP/Boost/boost/random/uniform_01.hpp @ 857

Revision 857, 2.9 KB checked in by igarcia, 19 years ago (diff)
RevLine 
[857]1/* boost random/uniform_01.hpp header file
2 *
3 * Copyright Jens Maurer 2000-2001
4 * Distributed under the Boost Software License, Version 1.0. (See
5 * accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
7 *
8 * See http://www.boost.org for most recent version including documentation.
9 *
10 * $Id: uniform_01.hpp,v 1.18 2004/07/27 03:43:32 dgregor Exp $
11 *
12 * Revision history
13 *  2001-02-18  moved to individual header files
14 */
15
16#ifndef BOOST_RANDOM_UNIFORM_01_HPP
17#define BOOST_RANDOM_UNIFORM_01_HPP
18
19#include <iostream>
20#include <boost/config.hpp>
21#include <boost/limits.hpp>
22#include <boost/static_assert.hpp>
23
24namespace boost {
25
26// Because it is so commonly used: uniform distribution on the real [0..1)
27// range.  This allows for specializations to avoid a costly int -> float
28// conversion plus float multiplication
29template<class UniformRandomNumberGenerator, class RealType = double>
30class uniform_01
31{
32public:
33  typedef UniformRandomNumberGenerator base_type;
34  typedef RealType result_type;
35
36  BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
37
38#if !defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) && !(defined(BOOST_MSVC) && BOOST_MSVC <= 1300)
39  BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
40#endif
41
42  explicit uniform_01(base_type rng)
43    : _rng(rng),
44      _factor(result_type(1) /
45              (result_type((_rng.max)()-(_rng.min)()) +
46               result_type(std::numeric_limits<base_result>::is_integer ? 1 : 0)))
47  {
48  }
49  // compiler-generated copy ctor and copy assignment are fine
50
51  result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(0); }
52  result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(1); }
53  base_type& base() { return _rng; }
54  const base_type& base() const { return _rng; }
55  void reset() { }
56
57  result_type operator()() {
58    return result_type(_rng() - (_rng.min)()) * _factor;
59  }
60
61#if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
62  template<class CharT, class Traits>
63  friend std::basic_ostream<CharT,Traits>&
64  operator<<(std::basic_ostream<CharT,Traits>& os, const uniform_01& u)
65  {
66    os << u._rng;
67    return os;
68  }
69
70  template<class CharT, class Traits>
71  friend std::basic_istream<CharT,Traits>&
72  operator>>(std::basic_istream<CharT,Traits>& is, uniform_01& u)
73  {
74    is >> u._rng;
75    return is;
76  }
77#endif
78
79private:
80  typedef typename base_type::result_type base_result;
81  base_type _rng;
82  result_type _factor;
83};
84
85#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
86//  A definition is required even for integral static constants
87template<class UniformRandomNumberGenerator, class RealType>
88const bool uniform_01<UniformRandomNumberGenerator, RealType>::has_fixed_range;
89#endif
90
91} // namespace boost
92
93#endif // BOOST_RANDOM_UNIFORM_01_HPP
Note: See TracBrowser for help on using the repository browser.