📄 random.texi
字号:
@section OverviewThese are the basic random number generators (RNGs):@cindex RNGs@cindex Random Number Generators@cindex Random Number Generators overview@table @code@item UniformUniform reals on [0,1)@item NormalNormal with specified mean and variance@item ExponentialExponential with specified mean@item DiscreteUniformIntegers uniformly distributed over a specified range.@item BetaBeta distribution@item GammaGamma distribution@item FF distribution@end tableTo use these generators, you need to include some subset of these headers:@example#include <random/uniform.h>#include <random/normal.h>#include <random/exponential.h>#include <random/discrete-uniform.h>#include <random/beta.h>#include <random/gamma.h>#include <random/chisquare.h>#include <random/F.h>using namespace ranlib;@end exampleAll the generators are inside the namespace @strong{ranlib}, @cindex ranlibso a @strong{using namespace ranlib} directive is required (alternately, youcan write e.g. @code{ranlib::Uniform<>}).These generators are all class templates. The first template parameter isthe number type you want to generate: float, double or long double forcontinuous distributions, and integer for discrete distributions. Thisparameter defaults to @code{float} for continuous distributions,and @code{unsigned int} for discrete distributions.The constructors are:@exampleUniform();Normal(T mean, T standardDeviation);Exponential(T mean);DiscreteUniform(T n); // range is 0 .. n-1Beta(T a, T b);Gamma(T mean);ChiSquare(T df);F(T dfn, T dfd);@end examplewhere @code{T} is the first template parameter (@code{float}, @code{double},or @code{long double}). To obtain a random number, use the method@code{random()}. Here is an example of constructing and using a@code{Normal} generator:@example#include <random/normal.h>using namespace ranlib;void foo()@{ Normal<double> normalGen; double x = normalGen.random(); // x is a normal random number@}@end example@section Note: Parallel random number generatorsThe generators which Blitz++ provides are not suitable for parallelprograms. If you need parallel RNGs, you may find@uref{http://www.ncsa.uiuc.edu/Apps/SPRNG} useful.@section Seeding a random number generator@cindex Random Number Generators seedingYou may seed a random number generator using the member function@code{seed(unsigned int)}. @cindex seeding a RNGBy default, all randomnumber generators share the same underlying integer random number generator.So seeding one generator will seed them all. (Note: you can creategenerators with their own internal state; see the sections below). Youshould generally only seed a random number generator once, at the beginningof a program run. Here is an example of seeding with the system clock:@example#include <random/uniform.h>#include <time.h>using namespace ranlib;int main()@{ // At start of program, seed with the system time so we get // a different stream of random numbers each run. Uniform<float> x; x.seed((unsigned int)time(0)); // Rest of program ...@}@end exampleNote: you may be tempted to seed the random number generator from a staticinitializer. @strong{Don't do it!} Due to an oddity of C++, there is noguarantee on the order of static initialization when templates are involved.Hence, you may seed the RNG before its constructor is invoked, in which caseyour program will crash. If you don't know what a static initializer is,don't worry -- you're safe!@section Detailed description of RNGs@cindex Random Number Generators detailsThere are really two types of RNGs:@table @code@item Integer RNGs provide uniformly distributed, unsigned 32 bit integers.@cindex IRNGs @cindex Integer RNGs@item RNGsuse Integer RNGs to provide other kinds of random numbers. @end tableBy default, the Integer RNG used is a faithful adaptation of the MersenneTwister @code{MT19937} @cindex MersenneTwister due to Matsumoto andNishimura (see @cite{ACM Transactions on Modeling and Computer Simulation,@w{Vol. 8}, @w{No. 1}, @w{January 1998}, @w{pp 3-30}},@uref{http://www.math.keio.ac.jp/~matumoto/emt.html},@uref{http://www.acm.org/pubs/citations/journals/tomacs/1998-8-1/p3-matsumoto/}).This generator has a period of @iftex@math{2^{19937}-1}@end iftex@ifinfo@math{2^{19937}-1}@end ifinfo@ifhtml2^(19937)-1@end ifhtml, passed several stringentstatistical tests (including the@uref{http://stat.fsu.edu/~geo/diehard.html} tests), and has speedcomparable to other modern generators.@section Template parametersRNGs take three template parameters, all of which have default values.Using the @code{Uniform} RNG as an example, the template parameters of@code{Uniform<T, IRNG, stateTag>} are:@table @code@item Tis the type of random number to generate (one of @code{float},@code{double}, or @code{long double} for continuous distributions; aninteger type for discrete distributions). Note that generating double andlong double RNGs takes longer, because filling the entire mantissa withrandom bits requires several random integers. The default parameter formost generators is @code{float}.@item IRNGis the underlying Integer RNG to use. The default is MersenneTwister.@item stateTag@cindex @code{stateTag} (RNGs) is either @code{sharedState} or @code{independentState}. If@code{sharedState}, the IRNG is shared with other generators. If@code{independentState}, the RNG contains its own IRNG. The default issharedState. @end table@section Member functions@cindex Random Number Generators member functionsRNGs have these methods:@exampleT random();@end example@findex random()Returns a random number.@examplevoid seed(unsigned int);@end example@findex seed()Seeds the underlying IRNG. See above for an example of seedingwith the system timer.@section Detailed listing of RNGs@cindex Random Number Generators list ofTo save space in the below list, template parameters have been omitted andonly constructors are listed. The notation [a,b] means an interval whichincludes the endpoints a and b; (a,b) is an interval which does not includethe endpoints.@subsection @file{random/uniform.h}@findex random/uniform.h@exampleUniform<>()@end example@cindex Uniform RNGContinuous uniform distribution on [0,1). @exampleUniformClosedOpen<>()@end example@cindex UniformClosedOpen RNGContinuous uniform distribution on [0,1). Same as @code{Uniform<>}.@exampleUniformClosed<>()@end example@cindex UniformClosed RNGContinuous uniform distribution on [0,1].@exampleUniformOpen<>()@end example@cindex UniformOpen RNGContinuous uniform distribution on (0,1).@exampleUniformOpenClosed<>()@end example@cindex UniformOpenClosed RNGContinuous uniform distribution on (0,1].@subsection @file{random/normal.h}@exampleNormalUnit<>()@end example@cindex NormalUnit RNGContinuous normal distribution with mean 0 and variance 1.@exampleNormal<>(T mean, T standardDeviation)@end example@cindex Normal RNGContinuous normal distribution with specified mean and standard deviation. @subsection @file{random/exponential.h}@exampleExponentialUnit<>()@end example@cindex ExponentialUnit RNGContinuous exponential distribution with mean 1.@exampleExponential<>(T mean)@end example@cindex Exponential RNGContinuous exponential distribution with specified mean.@subsection @file{random/beta.h}@exampleBeta<>(T a, T b)@end example@cindex Beta RNGBeta distribution with parameters a and b. The mean of the distribution is@math{a/(a+b)} and its variance is @math{ab/((a+b)^2(a+b+1))}. Use themethod @code{setParameters(T a, T b)} to change the parameters.@subsection @file{random/chisquare.h}@exampleChiSquare<>(T df)@end example@cindex ChiSquare RNG@math{\chi^2} distribution with @code{df} degrees of freedom. The parameterdf must be positive. Use the method @code{setDF(T df)} to change thedegrees of freedom.@subsection @file{random/gamma.h}@exampleGamma<>(T mean)@end example@cindex Gamma RNGGamma distribution with specified mean. The mean mustbe positive. Use the method @code{setMean(T mean)} tochange the mean.@subsection @file{random/F.h}@exampleF<>(T numeratorDF, T denominatorDF)@end example@cindex F distribution RNGF distribution with numerator and denominator degreesof freedom specified. Both these parameters must bepositive. Use @code{setDF(T dfn, T dfd)} to change thedegrees of freedom. @subsection @file{random/discrete-uniform.h}@exampleDiscreteUniform<>(T n)@end example@cindex DiscreteUniform RNGDiscrete uniform distribution over @math{0, 1, \ldots, n-1}.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -