~ubuntu-branches/ubuntu/warty/aqsis/warty

« back to all changes in this revision

Viewing changes to boost/boost/random/uniform_01.hpp

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-08-24 07:25:04 UTC
  • Revision ID: james.westby@ubuntu.com-20040824072504-zf993vnevvisdsvb
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* boost random/uniform_01.hpp header file
 
2
 *
 
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,
 
8
 *
 
9
 * Jens Maurer makes no representations about the suitability of this
 
10
 * software for any purpose. It is provided "as is" without express or
 
11
 * implied warranty.
 
12
 *
 
13
 * See http://www.boost.org for most recent version including documentation.
 
14
 *
 
15
 * $Id: uniform_01.hpp,v 1.1 2004/02/27 03:16:46 pseudonym Exp $
 
16
 *
 
17
 * Revision history
 
18
 *  2001-02-18  moved to individual header files
 
19
 */
 
20
 
 
21
#ifndef BOOST_RANDOM_UNIFORM_01_HPP
 
22
#define BOOST_RANDOM_UNIFORM_01_HPP
 
23
 
 
24
#include <boost/config.hpp>
 
25
#include <boost/limits.hpp>
 
26
#include <boost/static_assert.hpp>
 
27
 
 
28
namespace boost {
 
29
 
 
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>
 
34
class uniform_01
 
35
{
 
36
public:
 
37
  typedef uniform_01<UniformRandomNumberGenerator, RealType> adaptor_type;
 
38
  typedef UniformRandomNumberGenerator base_type;
 
39
  typedef RealType result_type;
 
40
 
 
41
  BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
 
42
 
 
43
#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
 
44
  BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
 
45
#endif
 
46
 
 
47
  explicit uniform_01(base_type & rng)
 
48
    : _rng(&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)))
 
52
  {
 
53
  }
 
54
  // compiler-generated copy ctor and copy assignment are fine
 
55
 
 
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; }
 
60
  void reset() { }
 
61
 
 
62
  result_type operator()() {
 
63
    return result_type((*_rng)() - _rng->min()) * _factor;
 
64
  }
 
65
 
 
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; }
 
69
 
 
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&)
 
74
  {
 
75
    return os;
 
76
  }
 
77
 
 
78
  template<class CharT, class Traits>
 
79
  friend std::basic_istream<CharT,Traits>&
 
80
  operator>>(std::basic_istream<CharT,Traits>& is, uniform_01&)
 
81
  {
 
82
    return is;
 
83
  }
 
84
#endif
 
85
 
 
86
#else
 
87
  // Use a member function
 
88
  bool operator==(const uniform_01& rhs) const
 
89
  { return *_rng == *rhs._rng;  }
 
90
#endif
 
91
private:
 
92
  typedef typename base_type::result_type base_result;
 
93
  base_type * _rng;
 
94
  result_type _factor;
 
95
};
 
96
 
 
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;
 
101
#endif
 
102
 
 
103
} // namespace boost
 
104
 
 
105
#endif // BOOST_RANDOM_UNIFORM_01_HPP