concepts.qbk 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. [/
  2. / Copyright (c) 2009 Steven Watanabe
  3. /
  4. / Distributed under the Boost Software License, Version 1.0. (See
  5. / accompanying file LICENSE_1_0.txt or copy at
  6. / http://www.boost.org/LICENSE_1_0.txt)
  7. ]
  8. [section Introduction]
  9. Random numbers are required in a number of different problem domains, such as
  10. * numerics (simulation, Monte-Carlo integration)
  11. * games (non-deterministic enemy behavior)
  12. * security (key generation)
  13. * testing (random coverage in white-box tests)
  14. The Boost Random Number Generator Library provides a framework for random
  15. number generators with well-defined properties so that the generators can be
  16. used in the demanding numerics and security domains. For a general
  17. introduction to random numbers in numerics, see
  18. [:"Numerical Recipes in C: The art of scientific computing", William H. Press,
  19. Saul A. Teukolsky, William A. Vetterling, Brian P. Flannery, 2nd ed., 1992,
  20. pp. 274-328]
  21. Depending on the requirements of the problem domain, different variations of
  22. random number generators are appropriate:
  23. * non-deterministic random number generator
  24. * pseudo-random number generator
  25. * quasi-random number generator
  26. All variations have some properties in common, the concepts (in the STL
  27. sense) is called __UniformRandomNumberGenerator. This
  28. concept will be defined in a subsequent section.
  29. The goals for this library are the following:
  30. * allow easy integration of third-party random-number generators
  31. * provide easy-to-use front-end classes which model popular distributions
  32. * provide maximum efficiency
  33. [endsect]
  34. [section Uniform Random Number Generator]
  35. A uniform random number generator provides a
  36. sequence of random numbers uniformly distributed on a given range. The
  37. range can be compile-time fixed or available (only) after run-time construction
  38. of the object.
  39. The /tight lower bound/ of some (finite) set S is the (unique) member l in S, so
  40. that for all v in S, l <= v holds. Likewise, the /tight upper bound/ of some
  41. (finite) set S is the (unique) member u in S, so that for all v in S, v <= u
  42. holds.
  43. In the following table, X denotes a number generator class returning objects
  44. of type T, and v is a const value of X.
  45. [table UniformRandomNumberGenerator requirements
  46. [[expression] [return type] [pre/post-condition]]
  47. [[`X::result_type`] [`T`] [`std::numeric_limits<T>::is_specialized` is
  48. `true`, `T` is __LessThanComparable]]
  49. [[`u.operator()()`] [`T`] [-]]
  50. [[`v.min()`] [`T`] [tight lower bound on the set of all values returned by
  51. `operator()`. The return value of this function shall not
  52. change during the lifetime of the object.]]
  53. [[`v.max()`] [`T`] [if `std::numeric_limits<T>::is_integer`, tight upper
  54. bound on the set of all values returned by `operator()`,
  55. otherwise, the smallest representable number larger than
  56. the tight upper bound on the set of all values returned
  57. by `operator()`. In any case, the return value of this
  58. function shall not change during the lifetime of the
  59. object.]]
  60. ]
  61. The member functions `min`, `max`, and `operator()` shall have amortized
  62. constant time complexity.
  63. [note For integer generators (i.e. integer `T`), the generated values `x`
  64. fulfill `min() <= x <= max()`, for non-integer generators (i.e. non-integer
  65. `T`), the generated values `x` fulfill `min() <= x < max()`.
  66. Rationale: The range description with min and max serves two purposes. First,
  67. it allows scaling of the values to some canonical range, such as [0..1).
  68. Second, it describes the significant bits of the values, which may be
  69. relevant for further processing.
  70. The range is a closed interval \[min,max\] for integers, because the underlying
  71. type may not be able to represent the half-open interval \[min,max+1). It is
  72. a half-open interval \[min, max) for non-integers, because this is much more
  73. practical for borderline cases of continuous distributions.]
  74. [note The __UniformRandomNumberGenerator concept does not require
  75. `operator()(long)` and thus it does not fulfill the `RandomNumberGenerator`
  76. (std:25.2.11 \[lib.alg.random.shuffle\]) requirements. Use the
  77. __random_number_generator adapter for that.
  78. Rationale: `operator()(long)` is not provided, because mapping the output of
  79. some generator with integer range to a different integer range is not trivial.]
  80. [endsect]
  81. [section Non-deterministic Uniform Random Number Generator]
  82. A non-deterministic uniform random number generator is a
  83. __UniformRandomNumberGenerator that is based on some stochastic process.
  84. Thus, it provides a sequence of truly-random numbers. Examples for such
  85. processes are nuclear decay, noise of a Zehner diode, tunneling of quantum
  86. particles, rolling a die, drawing from an urn, and tossing a coin. Depending
  87. on the environment, inter-arrival times of network packets or keyboard events
  88. may be close approximations of stochastic processes.
  89. The class __random_device is a model for a non-deterministic random number
  90. generator.
  91. [note This type of random-number generator is useful for security
  92. applications, where it is important to prevent an outside attacker from
  93. guessing the numbers and thus obtaining your encryption or authentication key.
  94. Thus, models of this concept should be cautious not to leak any information,
  95. to the extent possible by the environment. For example, it might be advisable
  96. to explicitly clear any temporary storage as soon as it is no longer needed.]
  97. [endsect]
  98. [section Pseudo-Random Number Generator]
  99. A pseudo-random number generator is a __UniformRandomNumberGenerator which
  100. provides a deterministic sequence of pseudo-random numbers, based on some
  101. algorithm and internal state.
  102. [classref boost::random::linear_congruential_engine
  103. Linear congruential] and [classref boost::random::inversive_congruential_engine
  104. inversive congruential] generators are examples of such [prng pseudo-random
  105. number generators]. Often, these generators are very sensitive to their
  106. parameters. In order to prevent wrong implementations from being used, an
  107. external testsuite should check that the generated sequence and the validation
  108. value provided do indeed match.
  109. Donald E. Knuth gives an extensive overview on pseudo-random number generation
  110. in his book "The Art of Computer Programming, Vol. 2, 3rd edition,
  111. Addison-Wesley, 1997". The descriptions for the specific generators contain
  112. additional references.
  113. [note Because the state of a pseudo-random number generator is necessarily
  114. finite, the sequence of numbers returned by the generator will loop
  115. eventually.]
  116. In addition to the __UniformRandomNumberGenerator requirements,
  117. a pseudo-random number generator has some additional requirements. In the
  118. following table, `X` denotes a pseudo-random number generator class,
  119. `u` is a value of `X`, `i` is a value of integral type, `s` is a value
  120. of a type which models __SeedSeq, and `j` a value of
  121. type `unsigned long long`.
  122. [table PseudoRandomNumberGenerator requirements
  123. [[expression] [return type] [pre/post-condition]]
  124. [[`X()`] [-] [creates a generator with a default seed.]]
  125. [[`X(i)`] [-] [creates a generator seeding it with the integer `i`.]]
  126. [[`X(s)`] [-] [creates a generator setting its initial state from the
  127. __SeedSeq `s`.]]
  128. [[`u.seed(...)`] [`void`] [sets the current state to be identical to the
  129. state that would be created by the corresponding
  130. constructor.]]
  131. [[`u.discard(j)`] [`void`] [Advances the generator by `j` steps as if by
  132. `j` calls to `u()`.]]
  133. ]
  134. Classes which model a pseudo-random number generator shall also model
  135. __EqualityComparable, i.e. implement `operator==`. Two pseudo-random number
  136. generators are defined to be /equivalent/ if they both return an identical
  137. sequence of numbers starting from a given state.
  138. Classes which model a pseudo-random number generator shall also model the
  139. __Streamable concept, i.e. implement `operator<<` and `operator>>`.
  140. `operator<<` writes all current state of the pseudo-random number generator
  141. to the given `ostream` so that `operator>>` can restore the state at a later
  142. time. The state shall be written in a platform-independent manner, but it is
  143. assumed that the `locales` used for writing and reading be the same. The
  144. pseudo-random number generator with the restored state and the original at
  145. the just-written state shall be equivalent.
  146. Classes which model a pseudo-random number generator should also model the
  147. __CopyConstructible and __Assignable concepts. However, note that the
  148. sequences of the original and the copy are strongly correlated (in fact,
  149. they are identical), which may make them unsuitable for some problem domains.
  150. Thus, copying pseudo-random number generators is discouraged; they should
  151. always be passed by (non-const) reference.
  152. The classes __rand48, __minstd_rand, and __mt19937 are models for a
  153. pseudo-random number generator.
  154. [note This type of random-number generator is useful for numerics, games and
  155. testing. The non-zero arguments constructor(s) and the `seed()` member
  156. function(s) allow for a user-provided state to be installed in the generator.
  157. This is useful for debugging Monte-Carlo algorithms and analyzing particular
  158. test scenarios. The __Streamable concept allows to save/restore the state of
  159. the generator, for example to re-run a test suite at a later time.]
  160. [endsect]
  161. [section Quasi-Random Number Generator]
  162. A quasi-random number generator is a __UniformRandomNumberGenerator which
  163. provides a deterministic sequence of quasi-random numbers, based on some
  164. algorithm and internal state. [classref boost::random::niederreiter_base2_engine
  165. Niederreiter base 2] generator is an example of such a [qrng quasi-random
  166. number generator]. The "quasi" modifier is used to denote more clearly that the
  167. values produced by such a generator are neither random nor pseudo-random, but
  168. they form a low discrepancy sequence. The intuitive idea is that a low discrepancy
  169. sequence is more evenly distributed than a pseudo random sequence would be.
  170. For example, if we generate a low discrepancy sequence of 2D points on a square,
  171. this square would be covered more evenly, and the number of points falling to any
  172. part of the square would be proportional to the number of points in the whole square.
  173. Such sequences share some properties of random variables and in certain applications
  174. such as the quasi-Monte Carlo method their lower discrepancy is an important advantage.
  175. [note The quasi-Monte Carlo method uses a low-discrepancy sequence such as the
  176. Niederreiter base 2 sequence, the Sobol sequence, or the Faure sequence among the others.
  177. The advantage of using low-discrepancy sequences is a probabilistically faster rate of
  178. convergence. Quasi-Monte Carlo has a rate of convergence O(log(N)[sup s]/N),
  179. whereas the rate of convergence for the Monte Carlo method, which uses a pseudo-random sequence,
  180. is O(N[sup -0.5]).]
  181. Harold Niederreiter gives an extensive overview on random number generation
  182. and quasi-Monte Carlo methods in his book "Random number generation and
  183. quasi-Monte Carlo methods, Society for Industrial and Applied Mathematics, 1992".
  184. In addition to the __UniformRandomNumberGenerator requirements,
  185. a quasi-random number generator has some additional requirements. In the
  186. following table, `X` denotes a quasi-random number generator class, `u` is a value of `X`,
  187. `v` is a const value of `X`, and `j` a value of type `unsigned long long`.
  188. [table QuasiRandomNumberGenerator requirements
  189. [[expression] [return type] [pre/post-condition]]
  190. [[`X(s)`] [-] [creates an `s`-dimensional generator with a default seed. Dimension `s` is an integer no less than 1.]]
  191. [[`v.dimension()`] [std::size_t] [the dimension of quasi-random domain.]]
  192. [[`u.seed(i)`] [`void`] [seeds the generator with the integer `i`.]]
  193. [[`u.discard(j)`] [`void`] [Advances the generator by `j` steps as if by
  194. `j` calls to `u()`.]]
  195. ]
  196. [note The `operator()` returns a successive element of an `s`-dimensional (`s` = `v.dimension()`) vector
  197. at each invocation. When all elements are exhausted, `operator()` begins anew with the starting
  198. element of a subsequent `s`-dimensional vector.]
  199. Classes which model a quasi-random number generator shall also model
  200. __EqualityComparable, i.e. implement `operator==`. Two quasi-random number
  201. generators are defined to be /equivalent/ if they both return an identical
  202. sequence of numbers starting from a given state.
  203. Classes which model a quasi-random number generator shall also model the
  204. __Streamable concept, i.e. implement `operator<<` and `operator>>`.
  205. `operator<<` writes all current state of the quasi-random number generator
  206. to the given `ostream` so that `operator>>` can restore the state at a later
  207. time. The state shall be written in a platform-independent manner, but it is
  208. assumed that the `locales` used for writing and reading be the same. The
  209. quasi-random number generator with the restored state and the original at
  210. the just-written state shall be equivalent.
  211. Classes which model a quasi-random number generator should also model the
  212. __CopyConstructible and __Assignable concepts. However, note that the
  213. sequences of the original and the copy are strongly correlated (in fact,
  214. they are identical), which may make them unsuitable for some problem domains.
  215. Thus, copying quasi-random number generators is discouraged; they should
  216. always be passed by (non-const) reference.
  217. The classes __niederreiter_base2, __sobol, __faure are models for a quasi-random number generator.
  218. [endsect]
  219. [section Seed Sequence]
  220. A SeedSeq represents a sequence of values that can be used to
  221. set the initial state of a __PseudoRandomNumberGenerator.
  222. `i` and `j` are RandomAccessIterators whose `value_type` is
  223. an unsigned integer type with at least 32 bits.
  224. [table SeedSeq requirements
  225. [[expression] [return type] [pre/post-condition] [complexity]]
  226. [[`s.generate(i, j)`] [void] [stores 32-bit values to all the elements
  227. in the iterator range defined by `i` and `j`]
  228. [O(j - i)]]
  229. ]
  230. The class __seed_seq and every __UniformRandomNumberGenerator
  231. provided by the library are models of __SeedSeq.
  232. [endsect]
  233. [section Random Distribution]
  234. A random distribution produces random numbers distributed according to some
  235. distribution, given uniformly distributed random values as input. In the
  236. following table, `X` denotes a random distribution class returning objects of
  237. type `T`, `u` is a value of `X`, `x` and `y` are (possibly const) values of
  238. `X`, `P` is the `param_type` of the distribution, `p` is a value of `P`, and
  239. `e` is an lvalue of an arbitrary type that meets the requirements of a
  240. __UniformRandomNumberGenerator, returning values of type `U`.
  241. [table Random distribution requirements (in addition to CopyConstructible, and Assignable)
  242. [[expression] [return type] [pre/post-condition] [complexity]]
  243. [[`X::result_type`] [`T`] [-] [compile-time]]
  244. [[`X::param_type`] [`P`] [A type that stores the parameters of the
  245. distribution, but not any of the state used to
  246. generate random variates. `param_type` provides
  247. the same set of constructors and accessors as
  248. the distribution.]
  249. [compile-time]]
  250. [[`X(p)`] [`X`] [Initializes a distribution from its parameters]
  251. [O(size of state)]]
  252. [[`u.reset()`] [`void`] [subsequent uses of `u` do not depend on values
  253. produced by any engine prior to invoking `reset`.]
  254. [constant]]
  255. [[`u(e)`] [`T`] [the sequence of numbers returned by successive invocations
  256. with the same object `e` is randomly distributed with the
  257. probability density function of the distribution]
  258. [amortized constant number of invocations of `e`]]
  259. [[`u(e, p)`] [`T`] [Equivalent to X(p)(e), but may use a different (and
  260. presumably more efficient) implementation]
  261. [amortized constant number of invocations of `e` +
  262. O(size of state)]]
  263. [[`x.param()`] [`P`] [Returns the parameters of the distribution]
  264. [O(size of state)]]
  265. [[`x.param(p)`] [void] [Sets the parameters of the distribution]
  266. [O(size of state)]]
  267. [[`x.min()`] [`T`] [returns the minimum value of the distribution] [constant]]
  268. [[`x.max()`] [`T`] [returns the maximum value of the distribution] [constant]]
  269. [[`x == y`] [`bool`] [Indicates whether the two distributions will produce
  270. identical sequences of random variates if given
  271. equal generators]
  272. [O(size of state)]]
  273. [[`x != y`] [`bool`] [`!(x == y)`] [O(size of state)]]
  274. [[`os << x`] [`std::ostream&`] [writes a textual representation for the
  275. parameters and additional internal data of
  276. the distribution `x` to `os`.
  277. post: The `os.fmtflags` and fill character
  278. are unchanged.]
  279. [O(size of state)]]
  280. [[`is >> u`] [`std::istream&`] [restores the parameters and additional
  281. internal data of the distribution `u`.
  282. pre: `is` provides a textual representation
  283. that was previously written by `operator<<`
  284. post: The `is.fmtflags` are unchanged.]
  285. [O(size of state)]]
  286. ]
  287. Additional requirements: The sequence of numbers produced by repeated
  288. invocations of `x(e)` does not change whether or not `os << x` is invoked
  289. between any of the invocations `x(e)`. If a textual representation is written
  290. using `os << x` and that representation is restored into the same or a
  291. different object `y` of the same type using `is >> y`, repeated invocations
  292. of `y(e)` produce the same sequence of random numbers as would repeated
  293. invocations of `x(e)`.
  294. [endsect]