2 * Copyright 2008-2013 NVIDIA Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <thrust/random/uniform_real_distribution.h>
26 template<typename RealType>
27 uniform_real_distribution<RealType>
28 ::uniform_real_distribution(RealType a, RealType b)
31 } // end uniform_real_distribution::uniform_real_distribution()
33 template<typename RealType>
34 uniform_real_distribution<RealType>
35 ::uniform_real_distribution(const param_type &parm)
38 } // end uniform_real_distribution::uniform_real_distribution()
40 template<typename RealType>
41 void uniform_real_distribution<RealType>
44 } // end uniform_real_distribution::reset()
46 template<typename RealType>
47 template<typename UniformRandomNumberGenerator>
48 typename uniform_real_distribution<RealType>::result_type
49 uniform_real_distribution<RealType>
50 ::operator()(UniformRandomNumberGenerator &urng)
52 return operator()(urng, m_param);
53 } // end uniform_real::operator()()
55 template<typename RealType>
56 template<typename UniformRandomNumberGenerator>
57 typename uniform_real_distribution<RealType>::result_type
58 uniform_real_distribution<RealType>
59 ::operator()(UniformRandomNumberGenerator &urng,
60 const param_type &parm)
62 // call the urng & map its result to [0,1)
63 result_type result = static_cast<result_type>(urng() - UniformRandomNumberGenerator::min);
65 // adding one to the denominator ensures that the interval is half-open at 1.0
66 // XXX adding 1.0 to a potentially large floating point number seems like a bad idea
67 // XXX OTOH adding 1 to what is potentially UINT_MAX also seems like a bad idea
68 // XXX we could statically check if 1u + (max - min) is representable and do that, otherwise use the current implementation
69 result /= (result_type(1) + static_cast<result_type>(UniformRandomNumberGenerator::max - UniformRandomNumberGenerator::min));
71 return (result * (parm.second - parm.first)) + parm.first;
72 } // end uniform_real::operator()()
74 template<typename RealType>
75 typename uniform_real_distribution<RealType>::result_type
76 uniform_real_distribution<RealType>
80 } // end uniform_real::a()
82 template<typename RealType>
83 typename uniform_real_distribution<RealType>::result_type
84 uniform_real_distribution<RealType>
87 return m_param.second;
88 } // end uniform_real_distribution::b()
90 template<typename RealType>
91 typename uniform_real_distribution<RealType>::param_type
92 uniform_real_distribution<RealType>
96 } // end uniform_real_distribution::param()
98 template<typename RealType>
99 void uniform_real_distribution<RealType>
100 ::param(const param_type &parm)
103 } // end uniform_real_distribution::param()
105 template<typename RealType>
106 typename uniform_real_distribution<RealType>::result_type
107 uniform_real_distribution<RealType>
108 ::min THRUST_PREVENT_MACRO_SUBSTITUTION (void) const
111 } // end uniform_real_distribution::min()
113 template<typename RealType>
114 typename uniform_real_distribution<RealType>::result_type
115 uniform_real_distribution<RealType>
116 ::max THRUST_PREVENT_MACRO_SUBSTITUTION (void) const
119 } // end uniform_real_distribution::max()
122 template<typename RealType>
123 bool uniform_real_distribution<RealType>
124 ::equal(const uniform_real_distribution &rhs) const
126 return m_param == rhs.param();
130 template<typename RealType>
131 template<typename CharT, typename Traits>
132 std::basic_ostream<CharT,Traits>&
133 uniform_real_distribution<RealType>
134 ::stream_out(std::basic_ostream<CharT,Traits> &os) const
136 typedef std::basic_ostream<CharT,Traits> ostream_type;
137 typedef typename ostream_type::ios_base ios_base;
139 // save old flags and fill character
140 const typename ios_base::fmtflags flags = os.flags();
141 const CharT fill = os.fill();
143 const CharT space = os.widen(' ');
144 os.flags(ios_base::dec | ios_base::fixed | ios_base::left);
147 os << a() << space << b();
149 // restore old flags and fill character
156 template<typename RealType>
157 template<typename CharT, typename Traits>
158 std::basic_istream<CharT,Traits>&
159 uniform_real_distribution<RealType>
160 ::stream_in(std::basic_istream<CharT,Traits> &is)
162 typedef std::basic_istream<CharT,Traits> istream_type;
163 typedef typename istream_type::ios_base ios_base;
166 const typename ios_base::fmtflags flags = is.flags();
168 is.flags(ios_base::skipws);
170 is >> m_param.first >> m_param.second;
178 template<typename RealType>
179 bool operator==(const uniform_real_distribution<RealType> &lhs,
180 const uniform_real_distribution<RealType> &rhs)
182 return thrust::random::detail::random_core_access::equal(lhs,rhs);
186 template<typename RealType>
187 bool operator!=(const uniform_real_distribution<RealType> &lhs,
188 const uniform_real_distribution<RealType> &rhs)
190 return !(lhs == rhs);
194 template<typename RealType,
195 typename CharT, typename Traits>
196 std::basic_ostream<CharT,Traits>&
197 operator<<(std::basic_ostream<CharT,Traits> &os,
198 const uniform_real_distribution<RealType> &d)
200 return thrust::random::detail::random_core_access::stream_out(os,d);
204 template<typename RealType,
205 typename CharT, typename Traits>
206 std::basic_istream<CharT,Traits>&
207 operator>>(std::basic_istream<CharT,Traits> &is,
208 uniform_real_distribution<RealType> &d)
210 return thrust::random::detail::random_core_access::stream_in(is,d);