Class thrust::random::linear_congruential_engine
A 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 x_i = (a * x_{i-1} + c) mod m
.
The following code snippet shows examples of use of a linear_congruential_engine
instance:
#include <thrust/random/linear_congruential_engine.h>
#include <iostream>
int main()
{
// 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;
}
Note: Inexperienced users should not use this class template directly. Instead, use minstd_rand
or minstd_rand0
.
Template Parameters:
UIntType
The type of unsigned integer to produce.a
The multiplier used in the generation algorithm.c
The increment used in the generation algorithm.m
The modulus used in the generation algorithm.
See:
#include <thrust/random/linear_congruential_engine.h>
template <typename UIntType, UIntType a, UIntType c, UIntType m> class thrust::random::linear_congruential_engine { public:
/* The type of the unsigned integer produced by this linear_congruential_engine
. */ typedef see below result_type;
static const result_type multiplier = see below;
static const result_type increment = see below;
static const result_type modulus = see below;
static const result_type min = see below;
static const result_type max = see below;
static const result_type default_seed = see below;
explicit _CCCL_HOST_DEVICE linear_congruential_engine(result_type s = default_seed) = default;
_CCCL_HOST_DEVICE void seed(result_type s = default_seed) = default;
_CCCL_HOST_DEVICE result_type operator()(void);
_CCCL_HOST_DEVICE void discard(unsigned long long z); };
Member Types
Typedef thrust::random::linear_congruential_engine::result_type
typedef UIntTyperesult_type;
The type of the unsigned integer produced by this linear_congruential_engine
.
Member Variables
Variable thrust::random::linear_congruential_engine::multiplier
static const result_type multiplier = a;
The multiplier used in the generation algorithm.
Variable thrust::random::linear_congruential_engine::increment
static const result_type increment = c;
The increment used in the generation algorithm.
Variable thrust::random::linear_congruential_engine::modulus
static const result_type modulus = m;
The modulus used in the generation algorithm.
Variable thrust::random::linear_congruential_engine::min
static const result_type min = c == 0u ? 1u : 0u;
The smallest value this linear_congruential_engine
may potentially produce.
Variable thrust::random::linear_congruential_engine::max
static const result_type max = m - 1u;
The largest value this linear_congruential_engine
may potentially produce.
Variable thrust::random::linear_congruential_engine::default_seed
static const result_type default_seed = 1u;
The default seed of this linear_congruential_engine
.
Member Functions
Function thrust::random::linear_congruential_engine::linear_congruential_engine
explicit _CCCL_HOST_DEVICE linear_congruential_engine(result_type s = default_seed) = default;
This constructor, which optionally accepts a seed, initializes a new linear_congruential_engine
.
Function Parameters: s
: The seed used to intialize this linear_congruential_engine's
state.
Function thrust::random::linear_congruential_engine::seed
_CCCL_HOST_DEVICE void seed(result_type s = default_seed) = default;
This method initializes this linear_congruential_engine's
state, and optionally accepts a seed value.
Function Parameters: s
: The seed used to initializes this linear_congruential_engine's
state.
Function thrust::random::linear_congruential_engine::operator()
_CCCL_HOST_DEVICE result_type operator()(void);
This member function produces a new random value and updates this linear_congruential_engine's
state.
Returns: A new random number.
Function thrust::random::linear_congruential_engine::discard
_CCCL_HOST_DEVICE void discard(unsigned long long z);
This member function advances this linear_congruential_engine's
state a given number of times and discards the results.
Note: This function is provided because an implementation may be able to accelerate it.
Function Parameters: z
: The number of random values to discard.