File size: 9,841 Bytes
7e2a408 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
/*
* Copyright 2008-2013 NVIDIA Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*! \file linear_congruential_engine.h
* \brief A linear congruential pseudorandom number engine.
*/
#pragma once
#include <thrust/detail/config.h>
#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)
# pragma GCC system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)
# pragma clang system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)
# pragma system_header
#endif // no system header
#include <iostream>
#include <thrust/detail/cstdint.h>
#include <thrust/random/detail/random_core_access.h>
#include <thrust/random/detail/linear_congruential_engine_discard.h>
THRUST_NAMESPACE_BEGIN
namespace random
{
/*! \addtogroup random_number_engine_templates Random Number Engine Class Templates
* \ingroup random
* \{
*/
/*! \class linear_congruential_engine
* \brief A \p linear_congruential_engine random number engine produces unsigned integer
* random numbers using a linear congruential random number generation algorithm.
*
* The generation algorithm has the form <tt>x_i = (a * x_{i-1} + c) mod m</tt>.
*
* \tparam UIntType The type of unsigned integer to produce.
* \tparam a The multiplier used in the generation algorithm.
* \tparam c The increment used in the generation algorithm.
* \tparam m The modulus used in the generation algorithm.
*
* \note Inexperienced users should not use this class template directly. Instead, use
* \p minstd_rand or \p minstd_rand0.
*
* The following code snippet shows examples of use of a \p linear_congruential_engine instance:
*
* \code
* #include <thrust/random/linear_congruential_engine.h>
* #include <iostream>
*
* int main(void)
* {
* // create a minstd_rand object, which is an instance of linear_congruential_engine
* thrust::minstd_rand rng1;
*
* // output some random values to cout
* std::cout << rng1() << std::endl;
*
* // a random value is printed
*
* // create a new minstd_rand from a seed
* thrust::minstd_rand rng2(13);
*
* // discard some random values
* rng2.discard(13);
*
* // stream the object to an iostream
* std::cout << rng2 << std::endl;
*
* // rng2's current state is printed
*
* // print the minimum and maximum values that minstd_rand can produce
* std::cout << thrust::minstd_rand::min << std::endl;
* std::cout << thrust::minstd_rand::max << std::endl;
*
* // the range of minstd_rand is printed
*
* // save the state of rng2 to a different object
* thrust::minstd_rand rng3 = rng2;
*
* // compare rng2 and rng3
* std::cout << (rng2 == rng3) << std::endl;
*
* // 1 is printed
*
* // re-seed rng2 with a different seed
* rng2.seed(7);
*
* // compare rng2 and rng3
* std::cout << (rng2 == rng3) << std::endl;
*
* // 0 is printed
*
* return 0;
* }
*
* \endcode
*
* \see thrust::random::minstd_rand
* \see thrust::random::minstd_rand0
*/
template<typename UIntType, UIntType a, UIntType c, UIntType m>
class linear_congruential_engine
{
public:
// types
/*! \typedef result_type
* \brief The type of the unsigned integer produced by this \p linear_congruential_engine.
*/
typedef UIntType result_type;
// engine characteristics
/*! The multiplier used in the generation algorithm.
*/
static const result_type multiplier = a;
/*! The increment used in the generation algorithm.
*/
static const result_type increment = c;
/*! The modulus used in the generation algorithm.
*/
static const result_type modulus = m;
/*! The smallest value this \p linear_congruential_engine may potentially produce.
*/
static const result_type min = c == 0u ? 1u : 0u;
/*! The largest value this \p linear_congruential_engine may potentially produce.
*/
static const result_type max = m - 1u;
/*! The default seed of this \p linear_congruential_engine.
*/
static const result_type default_seed = 1u;
// constructors and seeding functions
/*! This constructor, which optionally accepts a seed, initializes a new
* \p linear_congruential_engine.
*
* \param s The seed used to intialize this \p linear_congruential_engine's state.
*/
__host__ __device__
explicit linear_congruential_engine(result_type s = default_seed);
/*! This method initializes this \p linear_congruential_engine's state, and optionally accepts
* a seed value.
*
* \param s The seed used to initializes this \p linear_congruential_engine's state.
*/
__host__ __device__
void seed(result_type s = default_seed);
// generating functions
/*! This member function produces a new random value and updates this \p linear_congruential_engine's state.
* \return A new random number.
*/
__host__ __device__
result_type operator()(void);
/*! This member function advances this \p linear_congruential_engine's state a given number of times
* and discards the results.
*
* \param z The number of random values to discard.
* \note This function is provided because an implementation may be able to accelerate it.
*/
__host__ __device__
void discard(unsigned long long z);
/*! \cond
*/
private:
result_type m_x;
static void transition(result_type &state);
friend struct thrust::random::detail::random_core_access;
friend struct thrust::random::detail::linear_congruential_engine_discard;
__host__ __device__
bool equal(const linear_congruential_engine &rhs) const;
template<typename CharT, typename Traits>
std::basic_ostream<CharT,Traits>& stream_out(std::basic_ostream<CharT,Traits> &os) const;
template<typename CharT, typename Traits>
std::basic_istream<CharT,Traits>& stream_in(std::basic_istream<CharT,Traits> &is);
/*! \endcond
*/
}; // end linear_congruential_engine
/*! This function checks two \p linear_congruential_engines for equality.
* \param lhs The first \p linear_congruential_engine to test.
* \param rhs The second \p linear_congruential_engine to test.
* \return \c true if \p lhs is equal to \p rhs; \c false, otherwise.
*/
template<typename UIntType_, UIntType_ a_, UIntType_ c_, UIntType_ m_>
__host__ __device__
bool operator==(const linear_congruential_engine<UIntType_,a_,c_,m_> &lhs,
const linear_congruential_engine<UIntType_,a_,c_,m_> &rhs);
/*! This function checks two \p linear_congruential_engines for inequality.
* \param lhs The first \p linear_congruential_engine to test.
* \param rhs The second \p linear_congruential_engine to test.
* \return \c true if \p lhs is not equal to \p rhs; \c false, otherwise.
*/
template<typename UIntType_, UIntType_ a_, UIntType_ c_, UIntType_ m_>
__host__ __device__
bool operator!=(const linear_congruential_engine<UIntType_,a_,c_,m_> &lhs,
const linear_congruential_engine<UIntType_,a_,c_,m_> &rhs);
/*! This function streams a linear_congruential_engine to a \p std::basic_ostream.
* \param os The \p basic_ostream to stream out to.
* \param e The \p linear_congruential_engine to stream out.
* \return \p os
*/
template<typename UIntType_, UIntType_ a_, UIntType_ c_, UIntType_ m_,
typename CharT, typename Traits>
std::basic_ostream<CharT,Traits>&
operator<<(std::basic_ostream<CharT,Traits> &os,
const linear_congruential_engine<UIntType_,a_,c_,m_> &e);
/*! This function streams a linear_congruential_engine in from a std::basic_istream.
* \param is The \p basic_istream to stream from.
* \param e The \p linear_congruential_engine to stream in.
* \return \p is
*/
template<typename UIntType_, UIntType_ a_, UIntType_ c_, UIntType_ m_,
typename CharT, typename Traits>
std::basic_istream<CharT,Traits>&
operator>>(std::basic_istream<CharT,Traits> &is,
linear_congruential_engine<UIntType_,a_,c_,m_> &e);
/*! \} // random_number_engine_templates
*/
/*! \addtogroup predefined_random
* \{
*/
// XXX the type N2111 used here was uint_fast32_t
/*! \typedef minstd_rand0
* \brief A random number engine with predefined parameters which implements a version of
* the Minimal Standard random number generation algorithm.
* \note The 10000th consecutive invocation of a default-constructed object of type \p minstd_rand0
* shall produce the value \c 1043618065 .
*/
typedef linear_congruential_engine<thrust::detail::uint32_t, 16807, 0, 2147483647> minstd_rand0;
/*! \typedef minstd_rand
* \brief A random number engine with predefined parameters which implements a version of
* the Minimal Standard random number generation algorithm.
* \note The 10000th consecutive invocation of a default-constructed object of type \p minstd_rand
* shall produce the value \c 399268537 .
*/
typedef linear_congruential_engine<thrust::detail::uint32_t, 48271, 0, 2147483647> minstd_rand;
/*! \} // predefined_random
*/
} // end random
// import names into thrust::
using random::linear_congruential_engine;
using random::minstd_rand;
using random::minstd_rand0;
THRUST_NAMESPACE_END
#include <thrust/random/detail/linear_congruential_engine.inl>
|