diff --git a/index.html b/index.html new file mode 100644 index 0000000..6129202 --- /dev/null +++ b/index.html @@ -0,0 +1,84 @@ + + + + + + +Boost Random Number Library + + + + + + + + + + + + + +
c++boost.gif (8819 bytes)HomeLibrariesPeopleFAQMore
+

Boost Random Number Library

+By Jens Maurer +

You should read the concepts documentation +before moving on.

+

For a quick start, it may be sufficient to have a look at random_demo.cpp. +

  + + + + + + + + + + + + + +
HeaderContents
<boost/random.hpp> +

documentation for generators
+ documentation for distributions
+ documentation for miscellaneous classes

Pseudo-random number generators (linear congruential, inversive + congruential, and others), distribution functions (uniform, normal, + exponential, and others) and miscellaneous decorator classes such as random_number_generator + for use in simulations, games, and testing.
<boost/nondet_random.hpp> +

documentation

Non-deterministic random number generators.
+  +

An extensive test suite for the pseudo-random number generators and +distributions is available as random_test.cpp.

+

Additional test programs include statistic_test.cpp +and histogram.cpp.

+

Rationale

+The methods for generating and evaluating deterministic and non-deterministic +random numbers differ radically. Furthermore, due to the inherent deterministic +design of present-day computers, it is often difficult to implement +non-deterministic random number generation facilities. Thus, the random number +library is split into separate header files, mirroring the two different +application domains. +

History and Acknowledgements

+In November 1999, Jeet Sukumaran proposed a framework based on virtual +functions, and later sketched a template-based approach. Ed Brey pointed out +that Microsoft Visual C++ does not support in-class member initializations and +suggested the enum workaround. Dave Abrahams highlighted quantization issues.
+
+The first public release of this random number library materialized in March +2000 after extensive discussions on the boost mailing list. Many thanks to Beman +Dawes for his original min_rand class, portability fixes, documentation +suggestions, and general guidance. Harry Erwin sent a header file which provided +additional insight into the requirements. Ed Brey and Beman Dawes wanted an +iterator-like interface.
+
+Beman Dawes managed the formal review, during which Matthias Troyer, Csaba +Szepesvari, and Thomas Holenstein gave detailed comments. The reviewed version +became an official part of boost on 17 June 2000.
+
+Gary Powell contributed suggestions for code cleanliness.
+
+Jens Maurer, 2000-06-27 + + + + diff --git a/nondet_random.html b/nondet_random.html new file mode 100644 index 0000000..e3e5886 --- /dev/null +++ b/nondet_random.html @@ -0,0 +1,119 @@ + + + + + +Boost RNG Library - Non-Deterministic Random Number Generators + + + + +

c++boost.gif (8819 bytes)Header +<boost/nondet_random.hpp>

+ + + +

Header<boost/nondet_random.hpp> +Synopsis

+ +
+namespace boost {
+  class random_device;
+} // namespace boost
+
+ + +

Class random_device

+ +

Synopsis

+ +
+class random_device : noncopyable
+{
+public:
+  typedef unsigned int result_type;
+  static const bool has_fixed_range = true;
+  static const result_type min_value = /* implementation defined */;
+  static const result_type max_value = /* implementation defined */;
+  explicit random_device(const std::string& token = default_token);
+  ~random_device();
+  unsigned int operator()();
+};
+
+ +

Description

+ +Class random_device models a +non-deterministic random number +generator. +It uses one or more implementation-defined stochastic processes to +generate a sequence of uniformly distributed non-deterministic random +numbers. For those environments where a non-deterministic random +number generator is not available, class random_device +must not be implemented. See +
+"Randomness Recommendations for Security", D. Eastlake, S. +Crocker, J. Schiller, Network Working Group, RFC1750, December 1994 +
+for further discussions. + +

+Note: Some operating systems abstract the computer hardware +enough to make it difficult to non-intrusively monitor stochastic +processes. However, several do provide a special device for exactly +this purpose. It seems to be impossible to emulate the functionality +using Standard C++ only, so users should be aware that this class may +not be available on all platforms. + +

Members

+ +
explicit random_device(const std::string& token = default_token)
+ +Effects: Constructs a random_device, +optionally using the given token as an access +specification (for example, a URL) to some implementation-defined +service for monitoring a stochastic process. + +

Implementation Note for Linux

+ +On the Linux operating system, token is interpreted as a +filesystem path. It is assumed that this path denotes an operating +system pseudo-device which generates a stream of non-deterministic +random numbers. The pseudo-device should never signal an error or +end-of-file. Otherwise, std::ios_base::failure is +thrown. By default, random_device uses the +/dev/urandom pseudo-device to retrieve the random +numbers. Another option would be to specify the +/dev/random pseudo-device, which blocks on reads if the +entropy pool has no more random bits available. + + +

Performance

+ +The test program nondet_random_speed.cpp +measures the execution times of the +nondet_random.hpp implementation of the above +algorithms in a tight loop. The performance has been evaluated on a +Pentium Pro 200 MHz with gcc 2.95.2, Linux 2.2.13, glibc 2.1.2. + +

+ + + +
classtime per invocation [usec]
random_device92.0
+ +

+The measurement error is estimated at +/- 1 usec. + +

+


+Jens Maurer, 2000-03-05 + + + + \ No newline at end of file diff --git a/random-concepts.html b/random-concepts.html new file mode 100644 index 0000000..2a8d0d4 --- /dev/null +++ b/random-concepts.html @@ -0,0 +1,344 @@ + + + + + +Boost Random Number Library Concepts + + + + +

Random Number Generator Library Concepts

+ + +

Introduction

+ +Random numbers are required in a number of different problem domains, +such as + + +The Boost Random Number Generator Library provides a framework +for random number generators with well-defined properties so that the +generators can be used in the demanding numerics and security domains. + +For a general introduction to random numbers in numerics, see +
+"Numerical Recipes in C: The art of scientific computing", William +H. Press, Saul A. Teukolsky, William A. Vetterling, Brian P. Flannery, +2nd ed., 1992, pp. 274-328 +
+ +Depending on the requirements of the problem domain, different +variations of random number generators are appropriate: + + + +All variations have some properties in common, these concepts (in the +STL sense) are called NumberGenerator and +UniformRandomNumberGenerator. Each concept will be defined in a +subsequent section. + +

+ +The goals for this library are the following: +

+ + +

Number Generator

+ +A number generator is a function object (std:20.3 +[lib.function.objects]) that takes zero arguments. Each call to +operator() returns a number. + + +In the following table, X denotes a number generator +class returning objects of type T, and u is +a value of X. + +

+ + + + + + + + +
NumberGenerator +requirements
expressionreturn typepre/post-condition
X::result_typeTstd::numeric_limits<T>::is_specialized is true, +T is LessThanComparable
u.operator()()T-
+ +

+ +Note: The NumberGenerator requirements do not impose any +restrictions on the characteristics of the returned numbers. + + +

Uniform Random Number Generator

+ +A uniform random number generator is a NumberGenerator that provides a +sequence of random numbers uniformly distributed on a given range. +The range can be compile-time fixed or available (only) after run-time +construction of the object. + +

+The tight lower bound of some (finite) set S is the (unique) +member l in S, so that for all v in S, l <= v holds. Likewise, the +tight upper bound of some (finite) set S is the (unique) +member u in S, so that for all v in S, v <= u holds. + +

+In the following table, X denotes a number generator +class returning objects of type T, and v is +a const value of X. + +

+ + + + + + + + + + + + + + + +
UniformRandomNumberGenerator +requirements
expressionreturn typepre/post-condition
X::has_fixed_rangeboolcompile-time constant; if true, the range on which +the random numbers are uniformly distributed is known at compile-time +and members min_value and max_value +exist. Note: This flag may also be false due to +compiler limitations.
X::min_valueTcompile-time constant; min_value is equal to +v.min()
X::max_valueTcompile-time constant; max_value is equal to +v.max()
v.min()Ttight lower bound on the set of all values returned by +operator(). The return value of this function shall not +change during the lifetime of the object.
v.max()Tif std::numeric_limits<T>::is_integer, tight +upper bound on the set of all values returned by +operator(), otherwise, the smallest representable number +larger than the tight upper bound on the set of all values returned by +operator(). In any case, the return value of this +function shall not change during the lifetime of the +object.
+ +

+The member functions min, max, and +operator() shall have amortized constant time complexity. + +

+Note: For integer generators (i.e. integer T), +the generated values x fulfill min() <= x <= +max(), for non-integer generators (i.e. non-integer +T), the generated values x fulfill +min() <= x < max(). +
+Rationale: The range description with min and +max serves two purposes. First, it allows scaling of the +values to some canonical range, such as [0..1). Second, it describes +the significant bits of the values, which may be relevant for further +processing. +
+The range is a closed interval [min,max] for integers, because the +underlying type may not be able to represent the half-open interval +[min,max+1). It is a half-open interval [min, max) for non-integers, +because this is much more practical for borderline cases of continuous +distributions. +

+ +Note: The UniformRandomNumberGenerator concept does not +require operator()(long) and thus it does not fulfill the +RandomNumberGenerator (std:25.2.11 [lib.alg.random.shuffle]) +requirements. Use the +random_number_generator +adapter for that. +
+ +Rationale: operator()(long) is not provided, +because mapping the output of some generator with integer range to a +different integer range is not trivial. + + +

Non-deterministic Uniform Random Number +Generator

+ +A non-deterministic uniform random number generator is a +UniformRandomNumberGenerator that is based on some stochastic process. +Thus, it provides a sequence of truly-random numbers. Examples for +such processes are nuclear decay, noise of a Zehner diode, tunneling +of quantum particles, rolling a die, drawing from an urn, and tossing +a coin. Depending on the environment, inter-arrival times of network +packets or keyboard events may be close approximations of stochastic +processes. +

+ +The class +random_device +is a model for a non-deterministic random number generator. + +

+ +Note: This type of random-number generator is useful for +security applications, where it is important to prevent that an +outside attacker guesses the numbers and thus obtains your +encryption or authentication key. Thus, models of this concept should +be cautious not to leak any information, to the extent possible by the +environment. For example, it might be advisable to explicitly clear +any temporary storage as soon as it is no longer needed. + + +

Pseudo-Random Number Generator

+ +A pseudo-random number generator is a UniformRandomNumberGenerator +which provides a deterministic sequence of pseudo-random numbers, +based on some algorithm and internal state. Linear congruential and +inversive congruential generators are examples of such pseudo-random +number generators. Often, these generators are very sensitive to +their parameters. In order to prevent wrong implementations from +being used, an external testsuite should check that the generated +sequence and the validation value provided do indeed match. +

+ +Donald E. Knuth gives an extensive overview on pseudo-random number +generation in his book "The Art of Computer Programming, Vol. 2, 3rd +edition, Addison-Wesley, 1997". The descriptions for the specific +generators contain additional references. +

+ +Note: Because the state of a pseudo-random number generator +is necessarily finite, the sequence of numbers returned by the +generator will loop eventually. + +

+In addition to the UniformRandomNumberGenerator requirements, a +pseudo-random number generator has some additional requirements. In +the following table, X denotes a pseudo-random number +generator class returning objects of type T, +x is a value of T, u is a value +of X, and v is a const value of +X. + +

+ + + + + + + + + + + +
PseudoRandomNumberGenerator +requirements
expressionreturn typepre/post-condition
X()-creates a generator in some implementation-defined +state. Note: Several generators thusly created may possibly +produce dependent or identical sequences of random numbers.
explicit X(...)-creates a generator with user-provided state; the implementation +shall specify the the constructor argument(s)
u.seed(...)voidsets the current state according to the argument(s); at least +functions with the same signature as the non-default +constructor(s) shall be provided. +
v.validation(x)boolcompares the pre-computed and hardcoded 10001th element in the +generator's random number sequence with x. The generator +must have been constructed by its default constructor and +seed must not have been called for the validation to +be meaningful. +
+ +

+ +Note: The seed member function is similar to the +assign member function in STL containers. However, the +naming did not seem appropriate. + +

+ +Classes which model a pseudo-random number generator shall also model +EqualityComparable, i.e. implement operator==. Two +pseudo-random number generators are defined to be equivalent +if they both return an identical sequence of numbers starting from a +given state. +

+Classes which model a pseudo-random number generator should also model +the Streamable concept, i.e. implement operator<< +and operator>>. If so, +operator<< writes all current state of the +pseudo-random number generator to the given ostream so +that operator>> can restore the state at a later +time. The state shall be written in a platform-independent manner, +but it is assumed that the locales used for writing and +reading be the same. + +The pseudo-random number generator with the restored state and the +original at the just-written state shall be equivalent. +

+ +Classes which model a pseudo-random number generator may also model +the CopyConstructible and Assignable concepts. However, note that the +sequences of the original and the copy are strongly correlated (in +fact, they are identical), which may make them unsuitable for some +problem domains. Thus, copying pseudo-random number generators is +discouraged; they should always be passed by (non-const) +reference. + +

+ +The classes +rand48, +minstd_rand, +and +mt19937 +are models for a pseudo-random number generator. + +

+Note: This type of random-number generator is useful for +numerics, games and testing. The none-zero arguments constructor(s) +and the seed() member function(s) allow for a +user-provided state to be installed in the generator. This is useful +for debugging Monte-Carlo algorithms and analyzing particular test +scenarios. The Streamable concept allows to save/restore the state of +the generator, for example to re-run a test suite at a later time. + + +

Quasi-Random Number Generators

+ +A quasi-random number generator is a Number Generator which provides a +deterministic sequence of numbers, based on some algorithm and +internal state. The numbers do not have any statistical properties +(such as uniform distribution or independence of successive values). + +

+ +Note: Quasi-random number generators are useful for +Monte-Carlo integrations where specially crafted sequences of random +numbers will make the approximation converge faster. + +

+ +[Does anyone have a model?] + +

+


+Jens Maurer, 2000-02-23 + + + diff --git a/random-distributions.html b/random-distributions.html new file mode 100644 index 0000000..16541f6 --- /dev/null +++ b/random-distributions.html @@ -0,0 +1,738 @@ + + + + + + +Boost Random Number Library Distributions + + + + +

Random Number Library Distributions

+ + + +

Introduction

+ +In addition to the random number +generators, this library provides distribution functions which map +one distribution (often a uniform distribution provided by some +generator) to another. + +

+Usually, there are several possible implementations of any given +mapping. Often, there is a choice between using more space, more +invocations of the underlying source of random numbers, or more +time-consuming arithmetic such as trigonometric functions. This +interface description does not mandate any specific implementation. +However, implementations which cannot reach certain values of the +specified distribution or otherwise do not converge statistically to +it are not acceptable. + +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
distributionexplanationexample
uniform_smallintdiscrete uniform distribution on a small set of integers (much +smaller than the range of the underlying generator)drawing from an urn
uniform_intdiscrete uniform distribution on a set of integers; the underlying +generator may be called several times to gather enough randomness for +the outputdrawing from an urn
uniform_01continuous uniform distribution on the range [0,1); important +basis for other distributions-
uniform_realcontinuous uniform distribution on some range [min, max) of real +numbersfor the range [0, 2pi): randomly dropping a stick and measuring +its angle in radiants (assuming the angle is uniformly +distributed)
bernoulli_distributionBernoulli experiment: discrete boolean valued distribution with +configurable probabilitytossing a coin (p=0.5)
geometric_distributionmeasures distance between outcomes of repeated Bernoulli experimentsthrowing a die several times and counting the number of tries +until a "6" appears for the first time
triangle_distribution??
exponential_distributionexponential distributionmeasuring the inter-arrival time of alpha particles emitted by +radioactive matter
normal_distributioncounts outcomes of (infinitely) repeated Bernoulli experimentstossing a coin 10000 times and counting how many front sides are shown
lognormal_distributionlognormal distribution (sometimes used in simulations)measuring the job completion time of an assembly line worker
uniform_on_sphereuniform distribution on a unit sphere of arbitrary dimensionchoosing a random point on Earth (assumed to be a sphere) where to +spend the next vacations
+ +

+ +The template parameters of the distribution functions are always in +the order +

+ +

+All distribution functions satisfy the input iterator requirements +(std:24.1.1 [lib.input.iterators]) in addition to the NumberGenerator +requirements. After an invocation of operator(), the +effects of invocations of operator* are undefined until +the next call to operator++. + +

+In this description, I have refrained from documenting those members +in detail which are already defined in the +concept documentation. + + +

Synopsis of the distributions in header +<boost/random.hpp>

+ +
+namespace boost {
+  template<class UniformRandomNumberGenerator, class IntType = int>
+  class uniform_smallint;
+  template<class UniformRandomNumberGenerator, class IntType = int>
+  class uniform_int;
+  template<class UniformRandomNumberGenerator, class RealType = double>
+  class uniform_01;
+  template<class UniformRandomNumberGenerator, class RealType = double>
+  class uniform_real;
+
+  // discrete distributions
+  template<class UniformRandomNumberGenerator>
+  class bernoulli_distribution;
+  template<class UniformRandomNumberGenerator, class IntType = int>
+  class geometric_distribution;
+
+  // continuous distributions
+  template<class UniformRandomNumberGenerator, class RealType = double>
+  class triangle_distribution;
+  template<class UniformRandomNumberGenerator, class RealType = double>
+  class exponential_distribution;
+  template<class UniformRandomNumberGenerator, class RealType = double>
+  class normal_distribution;
+  template<class UniformRandomNumberGenerator, class RealType = double>
+  class lognormal_distribution;
+  template<class UniformRandomNumberGenerator, class RealType = double,
+    class Cont = std::vector<RealType> >
+  class uniform_on_sphere;
+}
+
+ +

Class template +uniform_smallint

+ +

Synopsis

+ +
+template<class UniformRandomNumberGenerator, class IntType = int>
+class uniform_smallint
+{
+public:
+  typedef UniformRandomNumberGenerator base_type;
+  typedef IntType result_type;
+  static const bool has_fixed_range = false;
+  uniform_smallint(base_type & rng, IntType min, IntType max);
+  result_type operator()();
+  result_type min() const;
+  result_type max() const;
+};
+
+ +

Description

+ +The distribution function uniform_smallint models a +uniform random number +generator. On each invocation, it returns a random integer value +uniformly distributed in the set of integer numbers {min, min+1, +min+2, ..., max}. It assumes that the desired range (max-min+1) is +small compared to the range of the underlying source of random +numbers and thus makes no attempt to limit quantization errors. +

+Let rout=(max-min+1) the desired range of integer numbers, +and let rbase be the range of the underlying source of +random numbers. Then, for the uniform distribution, the theoretical +probability for any number i in the range rout will be +pout(i) = 1/rout. Likewise, assume a uniform +distribution on rbase for the underlying source of random +numbers, i.e. pbase(i) = 1/rbase. Let +pout_s(i) denote the random distribution generated by +uniform_smallint. Then the sum over all i in +rout of (pout_s(i)/pout(i) +-1)2 shall not exceed +rout/rbase2 (rbase mod +rout)(rout - rbase mod +rout). +

+ +The template parameter UniformRandomNumberGenerator shall +denote a class which models a uniform random number generator. +Additionally, UniformRandomNumberGenerator::result_type +shall denote an integral type. The template parameter +IntType shall denote an integer-like value type. + +

+Note: The property above is the square sum of the relative +differences in probabilities between the desired uniform distribution +pout(i) and the generated distribution +pout_s(i). The property can be fulfilled with the +calculation (base_rng mod rout), as follows: Let r = +rbase mod rout. The base distribution on +rbase is folded onto the range rout. The +numbers i < r have assigned (rbase div +rout)+1 numbers of the base distribution, the rest has only +(rbase div rout). Therefore, +pout_s(i) = ((rbase div rout)+1) / +rbase for i < r and pout_s(i) = +(rbase div rout)/rbase otherwise. +Substituting this in the above sum formula leads to the desired +result. +

+Note: The upper bound for (rbase mod rout)(rout - rbase +mod rout) is rout2/4. Regarding the upper bound for the square +sum of the relative quantization error of rout3/(4*rbase2), it +seems wise to either choose rbase so that rbase > 10*rout2 or +ensure that rbase is divisible by rout. + + +

Members

+ +
uniform_smallint(base_type & rng, IntType min, IntType max)
+ +Effects: Constructs a uniform_smallint +functor with the uniform random number generator rng as +the underlying source of random numbers. min and +max are the lower and upper bounds of the output range, +respectively. + + +

Class template uniform_int

+ +

Synopsis

+ +
+template<class UniformRandomNumberGenerator, class IntType = int>
+class uniform_int
+{
+public:
+  typedef UniformRandomNumberGenerator base_type;
+  typedef IntType result_type;
+  static const bool has_fixed_range = false;
+  uniform_int(base_type & rng, IntType min, IntType max);
+  IntType operator()();
+  result_type min() const;
+  result_type max() const;
+};
+
+ +

Description

+ +The distribution function uniform_int models a +uniform random number +generator. On each invocation, it returns a random integer +value uniformly distributed in the set of integer numbers +{min, min+1, min+2, ..., max}. +

+ +The template parameter IntType shall denote an +integer-like value type. + +

Members

+ +
uniform_int(base_type & rng, IntType min, IntType max)
+ +Effects: Constructs a uniform_int functor +with the uniform random number generator rng as the +underlying source of random numbers. min and +max are the lower and upper bounds of the output range, +respectively. + +

+Note: Invocations of operator() may call the +underlying generator several times and concatenate the result to build +the required range. Thus, using this distribution with generators +such as linear congruential ones which tend to produce non-random bits +in some positions is strongly discouraged. + +

Class template uniform_01

+ +

Synopsis

+ +
+template<class UniformRandomNumberGenerator, class RealType = double>
+class uniform_01
+{
+public:
+  typedef UniformRandomNumberGenerator base_type;
+  typedef RealType result_type;
+  static const bool has_fixed_range = false;
+  explicit uniform_01(base_type & rng);
+  result_type operator()();
+  result_type min() const;
+  result_type max() const;
+};
+
+ +

Description

+ +The distribution function uniform_01 models a +uniform random number +generator. On each invocation, it returns a random floating-point +value uniformly distributed in the range [0..1). + +The value is computed using +std::numeric_limits<RealType>::digits random binary +digits, i.e. the mantissa of the floating-point value is completely +filled with random bits. [Note: Should this be configurable?] + +

+The template parameter RealType shall denote a float-like +value type with support for binary operators +, -, and /. It must be +large enough to hold floating-point numbers of value +rng.max()-rng.min()+1. +

+base_type::result_type must be a number-like value type, +it must support static_cast<> to +RealType and binary operator -. + +

+ +Note: The current implementation is buggy, because it may not +fill all of the mantissa with random bits. I'm unsure how to fill a +(to-be-invented) boost::bigfloat class with random bits +efficiently. It's probably time for a traits class. + +

Members

+ +
explicit uniform_01(base_type & rng)
+ +Effects: Constructs a uniform_01 functor +with the given uniform random number generator as the underlying +source of random numbers. + + +

Class template uniform_real

+ +

Synopsis

+ +
+template<class UniformRandomNumberGenerator, class RealType = double>
+class uniform_real
+{
+public:
+  typedef UniformRandomNumberGenerator base_type;
+  typedef RealType result_type;
+  static const bool has_fixed_range = false;
+  uniform_real(base_type & rng, RealType min, RealType max);
+  result_type operator()();
+  result_type min() const;
+  result_type max() const;
+};
+
+ +

Description

+ +The distribution function uniform_real models a +uniform random number +generator. On each invocation, it returns a random floating-point +value uniformly distributed in the range [min..max). The value is +computed using +std::numeric_limits<RealType>::digits random binary +digits, i.e. the mantissa of the floating-point value is completely +filled with random bits. +

+ +Note: The current implementation is buggy, because it may not +fill all of the mantissa with random bits. + + +

Members

+ +
explicit uniform_real(base_type & rng, RealType min, RealType max)
+ +Effects: Constructs a uniform_real +functor. rng specifies the uniform random number +generator to be used as the underlying source of random numbers, +min and max are the lower and upper bounds of +the output range. + + +

Class template +bernoulli_distribution

+ +

Synopsis

+ +
+template<class UniformRandomNumberGenerator>
+class bernoulli_distribution
+{
+public:
+  typedef UniformRandomNumberGenerator base_type;
+  typedef bool result_type;
+  bernoulli_distribution(base_type & rng, double q);
+  result_type operator()();
+};
+
+ +

Description

+ +Instantiations of class template bernoulli_distribution +model a number +generator. It transforms a uniform distribution into a Bernoulli +one. + +

Members

+ +
bernoulli_distribution(base_type & rng, double q)
+ +Effects: Constructs a +bernoulli_distribution functor with the uniform random +number generator rng as the underlying source of random +numbers. q is the parameter for the distribution. + +
result_type operator()()
+Returns: A random boolean value with p(true) = q and +p(false) = 1-q. For example, with q = 1/2 this can be interpreted as +tossing a coin. + + +

Class template +geometric_distribution

+ +

Synopsis

+
+template<class UniformRandomNumberGenerator, class IntType = int>
+class geometric_distribution
+{
+public:
+  typedef UniformRandomNumberGenerator base_type;
+  typedef IntType result_type;
+  geometric_distribution(base_type& rng, double q);
+  result_type operator()();
+};
+
+ + +

Description

+ +Instantiations of class template geometric_distribution +model a number +generator. It transforms a uniform distribution into a geometric +one. + + +

Members

+ +
geometric_distribution(base_type& rng, const result_type& q)
+ +Effects: Constructs a +geometric_distribution functor with the uniform random +number generator rng as the underlying source of random +numbers. q is the parameter for the distribution. +

+ +

result_type operator()()
+ +Returns: A random integer value i >= 1 with +p(i) = (1-q) * qi-1. For example, with q = 5/6 this can be +interpreted as the number of times one has to roll a die until a given +number shows up. + + +

Class template +triangle_distribution

+ +

Synopsis

+
+template<class UniformRandomNumberGenerator, class RealType = double>
+class triangle_distribution
+{
+public:
+  typedef UniformRandomNumberGenerator base_type;
+  typedef RealType result_type;
+  triangle_distribution(base_type& rng, result_type a, result_type b, result_type c);
+  result_type operator()();
+};
+
+ + +

Description

+ +Instantiations of class template triangle_distribution +model a number +generator. It transforms a uniform distribution into a triangle +one. + + +

Members

+ +
triangle_distribution(base_type& rng, result_type a, result_type b, result_type c)
+ +Effects: Constructs a +triangle_distribution functor with the uniform random +number generator rng as the underlying source of random +numbers. a, b, c are the parameters for the distribution. +

+ +

result_type operator()()
+ +Returns: A random floating-point value x +where a <= x <= c; x has a triangle +distribution, where b is the most probable value for +x. + + +

Class template +exponential_distribution

+ +

Synopsis

+
+template<class UniformRandomNumberGenerator, class RealType = double>
+class exponential_distribution
+{
+public:
+  typedef UniformRandomNumberGenerator base_type;
+  typedef RealType result_type;
+  exponential_distribution(base_type& rng, const result_type& lambda);
+  result_type operator()();
+};
+
+ +

Description

+ +Instantiations of class template exponential_distribution +model a number +generator. It transforms a uniform distribution into an +exponential one. + +

Members

+ +
exponential_distribution(base_type& rng, const result_type& lambda)
+ +Effects: Constructs an +exponential_distribution functor with the uniform random +number generator rng as the underlying source of random +numbers. lambda is the parameter for the distribution. +

+ +

result_type operator()()
+ +Returns: A random floating-point value x +> 0 with p(x) = lambda * exp(-lambda * +x). + + +

Class template +normal_distribution

+ +

Synopsis

+ +
+template<class UniformRandomNumberGenerator, class RealType = double>
+class normal_distribution
+{
+public:
+  typedef UniformRandomNumberGenerator base_type;
+  typedef RealType result_type;
+  explicit normal_distribution(base_type& rng, const result_type& mean = 0,
+			       const result_type& sigma = 1);
+  result_type operator()();
+};
+
+ +

Description

+ +Instantiations of class template normal_distribution +model a number +generator. It transforms a uniform distribution into a +normal (Gaussian) one. + +

Members

+ +
normal_distribution(base_type& rng, const result_type& mean = 0,
+		     const result_type& sigma = 1)
+ +Effects: Constructs a +normal_distribution functor with the uniform random +number generator rng as the underlying source of random +numbers. mean and sigma are the parameters for +the distribution. +

+ +

result_type operator()()
+ +Returns: A random floating-point value x +with p(x) = 1/sqrt(2*pi*sigma) * exp(- (x-mean)2 / +(2*sigma2) ). + + +

Class template +lognormal_distribution

+ +

Synopsis

+ +
+template<class UniformRandomNumberGenerator, class RealType = double>
+class lognormal_distribution
+{
+public:
+  typedef UniformRandomNumberGenerator base_type;
+  typedef RealType result_type;
+  explicit lognormal_distribution(base_type& rng, const result_type& mean,
+			          const result_type& sigma);
+  result_type operator()();
+};
+
+ +

Description

+ +Instantiations of class template lognormal_distribution +model a number +generator. It transforms a uniform distribution into a +lognormal one. + +

Members

+ +
lognormal_distribution(base_type& rng, const result_type& mean = 0,
+	   	       const result_type& sigma = 1)
+ +Effects: Constructs a +normal_distribution functor with the uniform random +number generator rng as the underlying source of random +numbers. mean and sigma are the parameters for +the distribution. +

+ +

result_type operator()()
+ +Returns: A random floating-point value x +with p(x) = 1/(x * sigma * sqrt(2*pi*sigma)) * exp(- +(log(x)-mean)2 / (2*sigma2) ) for x > 0. + + +

Class template +uniform_on_sphere

+ +

Synopsis

+ +
+template<class UniformRandomNumberGenerator, class RealType = double,
+  class Cont = std::vector<RealType> >
+class uniform_on_sphere
+{
+public:
+  typedef UniformRandomNumberGenerator base_type;
+  typedef Cont result_type;
+  explicit uniform_on_sphere(base_type & rng, int dim = 2);
+  const result_type & operator()();
+};
+
+ +

Description

+ +Instantiations of class template uniform_on_sphere model a +Generator (std:25.2.6 [lib.alg.generate]). It transforms a uniform +distribution into a uniform distribution on the unit sphere of +arbitrary dimension. The Cont template parameter must be +a STL-like container type with begin and end +operations returning non-const ForwardIterators of type +Cont::iterator. + +

Members

+ +
explicit uniform_on_sphere(base_type & rng, int dim = 2)
+ +Effects: Constructs a uniform_on_sphere +functor with the uniform random number generator rng as +the underlying source of random numbers. dim is the +dimension of the sphere. +

+ +

result_type operator()()
+ +Returns: A position on the unit sphere of +dim dimensions in cartesian coordinates. The positions +are uniformly distributed on the unit sphere. +

+Complexity: Proportional to the number of dimensions. + +

+


+Jens Maurer, 2000-06-14 + + + diff --git a/random-generators.html b/random-generators.html new file mode 100644 index 0000000..bb284e8 --- /dev/null +++ b/random-generators.html @@ -0,0 +1,780 @@ + + + + + + +Boost Random Number Library Generators + + + + +

Random Number Library Generators

+ + + +

Introduction

+ +This library provides several pseudo-random number generators. +The quality of a pseudo-random number generator crucially depends on +both the algorithm and its parameters. This library implements the +algorithms as class templates with template value parameters, hidden +in namespace detail. Any particular choice of parameters +is represented as the appropriately specializing typedef +in namespace boost. + +

+ +The following table gives an overview of some characteristics of the +generators. The cycle length is a rough estimate of the quality of +the generator; the approximate relative speed is a performance +measure, higher numbers mean faster random number generation. + +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
generatorlength of cyclememory +requirementsapproximate relative speedcomment
minstd_rand2**31-2sizeof(int32_t)0.4-
rand482**48-1sizeof(uint64_t)0.8-
lrand48 (C library)2**48-1-0.2global state
ecuyer1988approx. 2**612*sizeof(int32_t)0.2-
kreutzer1986?1368*sizeof(uint32_t)0.6-
hellekalek19952**31-1sizeof(int32_t)0.03good uniform distribution in several dimensions
mt11213b +2**11213-1352*sizeof(uint32_t)1good uniform distribution in up to 350 dimensions
mt19937 +2**19937-1625*sizeof(uint32_t)1good uniform distribution in up to 623 dimensions
+ +

+As observable from the table, there is generally a +quality/performance/memory trade-off to be decided upon when choosing +a random-number generator. The multitude of generators provided in +this library allows the application programmer to optimize the +trade-off with regard to his application domain. Additionally, +employing several fundamentally different random number generators for +a given application of Monte Carlo simulation will improve the +confidence in the results. + +

+Note: These random number generators are not indended for use +in applications where non-deterministic random numbers are required. +See nondet_random.html for a choice +of (hopefully) non-deterministic random number generators. + +

+In this description, I have refrained from documenting those members +in detail which are already defined in the +concept documentation. + + +

Synopsis of the generators in header +<boost/random.hpp>

+ +
+namespace boost {
+  namespace detail {
+    template<class IntType, IntType m>
+    class const_mod;
+    template<class IntType, IntType a, IntType c, IntType m, IntType val>
+    class linear_congruential;
+  }
+  class rand48;
+  typedef detail::linear_congruential< /* ... */ > minstd_rand0;
+  typedef detail::linear_congruential< /* ... */ > minstd_rand;
+
+  namespace detail {
+    template<class DataType, int n, int m, int r, DataType a, int u,
+        int s, DataType b, int t, DataType c, int l, IntType val>
+  class mersenne_twister;
+  }
+  typedef detail::mersenne_twister< /* ... */ > mt11213b;
+  typedef detail::mersenne_twister< /* ... */ > mt19937;
+} // namespace boost
+
+ + +

Class template +detail::const_mod

+ +

Synopsis

+ +
+template<class IntType, IntType m>
+class detail::const_mod
+{
+public:
+  template<IntType c>
+  static IntType add(IntType x);
+
+  template<IntType a>
+  static IntType mult(IntType x);
+
+  template<IntType a, IntType c>
+  static IntType mult_add(IntType x);
+
+  static IntType invert(IntType x);
+private:
+  const_mod();         // don't instantiate
+};
+
+ +

Description

+ +Class template const_mod provides functions performing +modular arithmetic, carefully avoiding overflows. All member +functions are static; there shall be no objects of type +const_mod<>. +

+ +The template parameter IntType shall denote an integral +type, m is the modulus. +

+ +Note: For modulo multiplications with large m, a trick allows +fast computation under certain conditions, see +

+"A more portable FORTRAN random number generator", Linus Schrage, ACM +Transactions on Mathematical Software, Vol. 5, No. 2, June 1979, pp. 132-138 +
+ + +

Member functions

+ +
template<IntType c> static IntType add(IntType x)
+ +Returns: (x+c) mod m + +
template<IntType a> static IntType mult(IntType x)
+ +Returns: (a*x) mod m + +
template<IntType a, IntType c> static IntType
+mult_add(IntType x)
+ +Returns: (a*x+c) mod m + +
static IntType invert(IntType x)
+ +Returns: i so that (a*i) mod m == 1 +
+Precondition: m is prime + + +

Class template +detail::linear_congruential

+ +

Synopsis

+ +
+template<class IntType, IntType a, IntType c, IntType m>
+class linear_congruential
+{
+public:
+  typedef IntType result_type;
+  static const IntType multiplier = a;
+  static const IntType increment = c;
+  static const IntType modulus = m;
+  static const bool has_fixed_range = true;
+  static const result_type min_value;
+  static const result_type max_value;
+  explicit linear_congruential_fixed(IntType x0 = 1);
+  // compiler-generated copy constructor and assignment operator are fine
+  void seed(IntType x0);
+  IntType operator()();
+};
+
+typedef detail::linear_congruential<long, 16807L, 0, 2147483647L,
+     1043618065L> minstd_rand0;
+typedef detail::linear_congruential<long, 48271L, 0, 2147483647L,
+     399268537L> minstd_rand;
+
+ +

Description

+ +Instantiations of class template linear_congruential +model a pseudo-random number +generator. Linear congruential pseudo-random number generators +are described in: +
+"Mathematical methods in large-scale computing units", D. H. Lehmer, +Proc. 2nd Symposium on Large-Scale Digital Calculating Machines, +Harvard University Press, 1951, pp. 141-146 +
+ +Let x(n) denote the sequence of numbers returned by +some pseudo-random number generator. Then for the linear congruential +generator, x(n+1) := (a * x(n) + c) mod m. Parameters for the +generator are x(0), a, c, m. + +The template parameter IntType shall denote an +integral type. It must be large enough to hold values a, c, and m. +The template parameters a and c must be smaller than m. + +

+ +Note: The quality of the generator crucially depends on the +choice of the parameters. User code should use one of the sensibly +parameterized generators such as minstd_rand instead. +
+For each choice of the parameters a, c, m, some distinct type is +defined, so that the static members do not interfere with +regard to the one definition rule. + +

Members

+ +
explicit linear_congruential(IntType x0 = 1)
+ +Effects: Constructs a +linear_congruential generator with x(0) := +x0. + +
void seed(IntType x0)
+ +Effects: Changes the current value x(n) of the +generator to x0. + +

Specializations

+ +The specialization minstd_rand0 was originally suggested +in +
+A pseudo-random number generator for the System/360, P.A. Lewis, +A.S. Goodman, J.M. Miller, IBM Systems Journal, Vol. 8, No. 2, 1969, +pp. 136-146 +
+ +It is examined more closely together with minstd_rand in +
+"Random Number Generators: Good ones are hard to find", Stephen +K. Park and Keith W. Miller, Communications of the ACM, Vol. 31, +No. 10, October 1988, pp. 1192-1201 +
+ + +

Class rand48

+ +

Synopsis

+
+class rand48 
+{
+public:
+  typedef int32_t result_type;
+  static const bool has_fixed_range = true;
+  static const int32_t min_value = 0;
+  static const int32_t max_value = 0x7fffffff;
+  
+  explicit rand48(int32_t x0 = 1);
+  explicit rand48(uint64_t x0);
+  // compiler-generated copy ctor and assignment operator are fine
+  void seed(int32_t x0);
+  void seed(uint64_t x0);
+  int32_t operator()();
+};
+
+ +

Description

+ +Class rand48 models a +
pseudo-random number +generator. It uses the linear congruential algorithm with the +parameters a = 0x5DEECE66D, c = 0xB, m = 2**48. It delivers identical +results to the lrand48() function available on some +systems (assuming lcong48 has not been called). +

+It is only available on systems where uint64_t is provided. + +

Constructors

+ +
rand48(int32_t x0)
+ +Effects: Constructs a rand48 generator +with x(0) := (x0 << 16) | 0x330e. + +
rand48(uint64_t x0)
+ +Effects: Constructs a rand48 generator +with x(0) := x0. + +

Seeding

+
void seed(int32_t x0)
+ +Effects: Changes the current value x(n) of the +generator to (x0 << 16) | 0x330e. + +
void seed(uint64_t x0)
+ +Effects: Changes the current value x(n) of the +generator to x0. + + +

Class template +detail::additive_combine

+ +

Synopsis

+ +
+template<class MLCG1, class MLCG2, typename MLCG1::result_type val>
+class detail::additive_combine
+{
+public:
+  typedef MLCG1 first_base;
+  typedef MLCG2 second_base;
+  typedef typename MLCG1::result_type result_type;
+  static const bool has_fixed_range = true;
+  static const result_type min_value = 1;
+  static const result_type max_value = MLCG1::max_value-1;
+  additive_combine();
+  additive_combine(typename MLCG1::result_type seed1, 
+		   typename MLCG2::result_type seed2);
+  result_type operator()();
+  bool validation(result_type x) const;
+};
+
+typedef detail::additive_combine<
+    detail::linear_congruential<int32_t, 40014, 0, 2147483563, 0>,
+    detail::linear_congruential<int32_t, 40692, 0, 2147483399, 0>,
+  /* unknown */ 0> ecuyer1988;
+
+
+ +

Description

+ +Instatiations of class template additive_combine model a +pseudo-random number +generator. It combines two multiplicative linear congruential +number generators, i.e. those with c = 0. It is described in +
+"Efficient and Portable Combined Random Number Generators", Pierre +L'Ecuyer, Communications of the ACM, Vol. 31, No. 6, June 1988, +pp. 742-749, 774 +
+ +The template parameters MLCG1 and MLCG2 +shall denote two different linear congruential number generators, each +with c = 0. Each invocation returns a random number X(n) := (MLCG1(n) +- MLCG2(n)) mod (m1 - 1), where m1 denotes the modulus of +MLCG1. + +

+The template parameter val is the validation value +checked by validation. + + +

Members

+ +
additive_combine()
+ +Effects: Constructs an additive_combine +generator using the default constructors of the two base generators. + +
additive_combine(typename MLCG1::result_type seed1, 
+ 		 typename MLCG2::result_type seed2)
+ +Effects: Constructs an additive_combine +generator, using seed1 and seed2 as the +constructor argument to the first and second base generator, +respectively. + + +

Specialization

+ +The specialization ecuyer1988 was suggested in the above +paper. + + +

Class template +detail::shuffle_output

+ +

Synopsis

+ +
+template<class UniformRandomNumberGenerator, int k, 
+  class IntType = typename UniformRandomNumberGenerator::result_type,
+  typename UniformRandomNumberGenerator::result_type val = 0>
+class detail::shuffle_output
+{
+public:
+  typedef UniformRandomNumberGenerator base_type;
+  typedef typename base_type::result_type result_type;
+  static const bool has_fixed_range = false;
+
+  shuffle_output();
+  template<class T> explicit shuffle_output(T seed);
+  explicit shuffle_output(const base_type & rng);
+  template<class T> void seed(T s);
+
+  result_type operator()();
+  result_type min() const;
+  result_type max() const;
+  bool validation(result_type) const;
+};
+
+ +

Description

+ +Instatiations of class template shuffle_output model a +pseudo-random number +generator. It mixes the output of some (usually linear +congruential) uniform random number generator to get better +statistical properties. According to Donald E. Knuth, "The Art of +Computer Programming, Vol. 2", the algorithm is described in +
+"Improving a poor random number generator", Carter Bays and +S.D. Durham, ACM Transactions on Mathematical Software, Vol. 2, 1979, +pp. 59-64. +
+The output of the base generator is buffered in an array of length +k. Every output X(n) has a second role: It gives an index into the +array where X(n+1) will be retrieved. Used array elements are +replaced with fresh output from the base generator. + +

+ +Template parameters are the base generator and the array length k, +which should be around 100. As an implementation detail, the template +parameter IntType shall denote an integer-like type which +is large enough to hold integer numbers of value k * +base_type::max(). The template parameter +val is the validation value checked by +validation. + + +

Members

+ +
shuffle_output()
+ +Effects: Constructs a shuffle_output +generator by invoking the default constructor of the base generator. +

+Complexity: Exactly k+1 invocations of the base +generator. + +

template<class T> explicit shuffle_output(T seed)
+ +Effects: Constructs a shuffle_output +generator by invoking the one-argument constructor of the base +generator with the parameter seed. +

+Complexity: Exactly k+1 invocations of the base +generator. + +

explicit shuffle_output(const base_type & rng)
+ +Precondition: The template argument +UniformRandomNumberGenerator shall denote a +CopyConstructible type. +

+Effects: Constructs a shuffle_output +generator by using a copy of the provided generator. +

+Complexity: Exactly k+1 invocations of the base +generator. + +

template<class T> void seed(T s)
+ +Effects: Invokes the one-argument seed +method of the base generator with the parameter seed and +re-initializes the internal buffer array. +

+Complexity: Exactly k+1 invocations of the base +generator. + + +

Specializations

+ +According to Harry Erwin (private e-mail), the specialization +kreutzer1986 was suggested in: +
+"System Simulation: programming Styles and Languages (International +Computer Science Series)", Wolfgang Kreutzer, Addison-Wesley, December +1986. +
+ + +

Class template +detail::inversive_congruential

+ +

Synopsis

+ +
+template<class IntType, IntType a, IntType b, IntType p>
+class detail::inversive_congruential
+{
+public:
+  typedef IntType result_type;
+  static const bool has_fixed_range = true;
+  static const result_type min_value = (b == 0 ? 1 : 0);
+  static const result_type max_value = p-1;
+  static const result_type multiplier = a;
+  static const result_type increment = b;
+  static const result_type modulus = p;
+  explicit inversive_congruential(IntType y0 = 1);
+  void seed(IntType y0);
+  IntType operator()();
+};
+
+typedef detail::inversive_congruential<int32_t, 9102, 2147483647-36884165, 2147483647> hellekalek1995;
+
+ +

Description

+ +Instantiations of class template inversive_congruential model a +pseudo-random number +generator. It uses the inversive congruential algorithm (ICG) +described in +
+"Inversive pseudorandom number generators: concepts, results and +links", Peter Hellekalek, In: "Proceedings of the 1995 Winter +Simulation Conference", C. Alexopoulos, K. Kang, W.R. Lilegdon, and +D. Goldsman (editors), 1995, pp. 255-262. +ftp://random.mat.sbg.ac.at/pub/data/wsc95.ps +
+ +The output sequence is defined by x(n+1) = (a*inv(x(n)) - b) (mod p), +where x(0), a, b, and the prime number p are parameters of the +generator. The expression inv(k) denotes the multiplicative inverse +of k in the field of integer numbers modulo p, with inv(0) := 0. + +

+ +The template parameter IntType shall denote a signed +integral type large enough to hold p; a, b, and p are the parameters +of the generators. +

+Note: The implementation currently uses the Euclidian +Algorithm to compute the multiplicative inverse. Therefore, the +inversive generators are about 10-20 times slower than the others (see +section"performance"). However, the paper +talks of only 3x slowdown, so the Euclidian Algorithm is probably not +optimal for calculating the multiplicative inverse. + + +

Members

+ +
inversive_congruential(IntType y0 = 1)
+ +Effects: Constructs an +inversive_congruential generator with +y0 as the initial state. + +
void seed(IntType y0)
+ +Effects: +Changes the current state to y0. + + +

Specialization

+ +The specialization hellekalek1995 was suggested in the +above paper. + + +

Class template +detail::mersenne_twister

+ +

Synopsis

+ +
+template<class DataType, int n, int m, int r, DataType a, int u,
+int s, DataType b, int t, DataType c, int l, IntType val>
+class detail::mersenne_twister
+{
+public:
+  typedef DataType result_type;
+  static const bool has_fixed_range = true;
+  static const result_type min_value;
+  static const result_type max_value;
+  mersenne_twister();
+  explicit mersenne_twister(DataType value);
+  template<class Generator> explicit mersenne_twister(Generator & gen);
+  // compiler-generated copy ctor and assignment operator are fine
+  void seed();
+  void seed(DataType value);
+  template<class Generator> void seed(Generator & gen);
+  result_type operator()();
+  bool validation(result_type) const;
+};
+
+typedef mersenne_twister<uint32_t,351,175,19,0xccab8ee7,11,7,0x31b6ab00,15,0xffe50000,17, /* unknown */ 0> mt11213b;
+typedef mersenne_twister<uint32_t,624,397,31,0x9908b0df,11,7,0x9d2c5680,15,0xefc60000,18, 3346425566U> mt19937;
+
+ +

Description

+ +Instantiations of class template mersenne_twister model a +pseudo-random number +generator. It uses the algorithm described in + +
+"Mersenne Twister: A 623-dimensionally equidistributed uniform +pseudo-random number generator", Makoto Matsumoto and Takuji Nishimura, +ACM Transactions on Modeling and Computer Simulation: Special Issue +on Uniform Random Number Generation, Vol. 8, No. 1, January 1998, +pp. 3-30. +http://www.math.keio.ac.jp/matumoto/emt.html +
+ +Note: The boost variant has been implemented from scratch +and does not derive from or use mt19937.c provided on the above WWW +site. However, it was verified that both produce identical output. +
+The quality of the generator crucially depends on the choice of the +parameters. User code should employ one of the sensibly parameterized +generators such as mt19937 instead. +
+The generator requires considerable amounts of memory for the storage +of its state array. For example, mt11213b requires about +1408 bytes and mt19937 requires about 2496 bytes. + +

Constructors

+ +
mersenne_twister()
+ +Effects: Constructs a mersenne_twister +and calls seed(). + +
explicit mersenne_twister(DataType value)
+ +Effects: Constructs a mersenne_twister +and calls seed(value). + +
template<class Generator> explicit mersenne_twister(Generator & gen)
+ +Effects: Constructs a mersenne_twister +and calls seed(gen). + +

Seeding

+ +
void seed()
+ +Effects: Calls seed(4357). + +
void seed(DataType value)
+ +Effects: Constructs a +linear_congruential<uint32_t, 69069, 0, 0, 0> +generator with the constructor parameter +value and calls seed with it. + +
template<class Generator> void seed(Generator & gen)
+ +Effects: Sets the state of this +mersenne_twister to the values returned by n +invocations of gen. + +

+ +Complexity: Exactly n invocations of gen. + +

Specializations

+ +The specializations mt11213b and mt19937 are +from the paper cited above. + + +

Performance

+ +The test program random_speed.cpp +measures the execution times of the +random.hpp implementation of the above +algorithms in a tight loop. The performance has been evaluated on a +Pentium Pro 200 MHz with gcc 2.95.2, Linux 2.2.13, glibc 2.1.2. + +

+ + + + + + + + + + + + + +
classtime per invocation [usec]
rand480.096
rand48 run-time configurable0.697
lrand48 glibc 2.1.20.844
minstd_rand0.174
ecuyer19880.445
kreutzer19860.249
hellekalek1995 (inversive)4.895
mt11213b0.165
mt199370.165
mt19937 original0.185
+ +

+The measurement error is estimated at +/- 10 nsec. + +

+


+Jens Maurer, 2000-06-13 + + + diff --git a/random-misc.html b/random-misc.html new file mode 100644 index 0000000..6ab2b5d --- /dev/null +++ b/random-misc.html @@ -0,0 +1,159 @@ + + + + + + +Boost Random Number Generator Library (Miscellaneous) + + + + +

Random Number Generator Library --- Miscellaneous Decorators

+ + + +

Introduction

+ +These decorator class templates allow adaption of the random number +generators and distribution functions to concepts found in the C++ +Standard Library, in particular the RandomNumberGenerator and the +InputIterator concepts. The latter adaption is useful, because the +the basic random number generators do not implement the InputIterator +requirements per se, in contrast to the distribution functions. + + +

Synopsis of miscellaneous decorators in +header <boost/random.hpp>

+ +
+namespace boost {
+  template<class UniformRandomNumberGenerator, class IntType = long>
+  class random_number_generator;
+  template<class Generator>
+  class generator_iterator;
+} // namespace boost
+
+ + +

Class template +random_number_generator

+ +

Synopsis

+
+template<class UniformRandomNumberGenerator, class IntType = long>
+class random_number_generator
+{
+public:
+  typedef UniformRandomNumberGenerator base_type;
+  typedef IntType argument_type;
+  typedef IntType result_type;
+  random_number_generator(base_type & rng);
+  result_type operator()(argument_type n);
+};
+
+ +

Description

+ +Instantiations of class template random_number_generator +model a RandomNumberGenerator (std:25.2.11 [lib.alg.random.shuffle]). +On each invocation, it returns a uniformly distributed integer in +the range [0..n). +

+The template parameter IntType shall denote some +integer-like value type. +

+ +Note: I consider it unfortunate that the C++ Standard uses +the name RandomNumberGenerator for something rather specific. + +

Members

+ +
random_number_generator(base_type & rng)
+ +Effects: Constructs a +random_number_generator functor with the given uniform +random number generator as the underlying source of random numbers. + +
result_type operator()(argument_type n)
+ +Returns: The value of +uniform_int<base_type>(rng, 0, n-1)(). + + +

Class template +generator_iterator

+ +

Synopsis

+
+template<class Generator>
+class generator_iterator
+  : equality_comparable<generator_iterator<Generator> >,
+  incrementable<generator_iterator<Generator> >,
+  dereferenceable<generator_iterator<Generator>,
+      typename Generator::result_type>
+{
+public:
+  typedef typename Generator::result_type value_type;
+  typedef std::ptrdiff_t difference_type;
+  typedef const typename Generator::result_type * pointer;
+  typedef const typename Generator::result_type & reference;
+  typedef std::input_iterator_tag iterator_category;
+
+  explicit generator_iterator(Generator & g);
+  generator_iterator& operator++();
+  reference operator*() const;
+  friend bool operator==(const generator_iterator<Generator>& x, 
+			 const generator_iterator<Generator>& y);
+};
+
+ +

Description

+ +Instantiations of class template generator_iterator +satisfy the input iterator requirements (std:24.1.1 +[lib.input.iterators]). It allows iterator-like access to a +generator, e.g. a NumberGenerator. Note that all distribution +functions now satisfy the input iterator requirements as-is. However, +the base generators do not. + +

Members

+ +
explicit generator_iterator(Generator & g)
+ +Effects: Constructs a generator_iterator +with g as the underlying generator. Invokes the +underlying generator functor. + +
generator_iterator& operator++()
+ +Effects: Invokes the underlying generator functor. +

+Returns: *this + +

reference operator*() const
+ +Returns: The value of the last invocation of the +underlying generator functor. + +

Overloaded global operators

+ +
bool operator==(const generator_iterator<Generator>& x, 
+	         const generator_iterator<Generator>& y)
+ +Returns: true iff the x and +y have been initialized with a reference to the same +generator functor and *x == *y. + + +

+


+Jens Maurer, 2000-03-06 + + +