Description of the standard library rand () function and the introduction of BLITZ ++ random number generation

xiaoxiao2021-03-06  61

Description of the standard library rand () function and the introduction of BLITZ random number generation

(Newsuppy, please indicate the source)

When we need to use random numbers in a certain task, we usually have used the RAND function of the standard library. Like this: SRAND (TIME (0)); // Time Seed

RAND ()% max_rand;

The RAND function of the standard library uses a linear integrated algorithm, which is a random number generation algorithm that generates a speed. In most cases, we can also meet our requirements, but for some special purpose applications, the random number generated by this algorithm is not line, such as some encryption algorithms, Monte Carlo points, etc. (Create a random password in .NET The random number cannot use the random raid random number of the Random class, and use the related random number generated in the system.security.cryptography namespace).

This linearity achievement can be found in many books. Here I gave an implementation in the THE C Programming Langurage 2nd, which is also the realization of the standard library random algorithm:

Unsigned long int next = 1;

/ * rand: return pseudo-random integer on 0..32767 * /

Int Rand (Void)

{

Next = next * 1103515245 12345;

Return (unsigned int) (Next / 65536)% 32768;

}

/ * SRAND: SET SEED for Rand () * /

Void Srand (unsigned int seed)

{

Next = seed;

}

This implementation problem is that the 32768 in the RAND function return line, which is defined as the RAND_MAX macro in the standard library, you can find the value of RAND_MAX in the stdlib.h header file (or cstdlib) of the Visualc and MingW32 compilers. 32768. That is to say, the random number of this algorithm is located in 0 - rand_max, and in the general compiler is 0-32768. Suppose your algorithm needs more than 30,000 random numbers, then use the RAND function to produce a random number of nearly 30 times!

So here I will briefly introduce how to generate classes using the random number in the BLITZ library. However, here I can't prove that the BLITZ random number algorithm is more advantageous relative to the standard library. The source code of the Blitz is open, you can fully understand the implementation of its random number algorithm.

Random number organizations in BLITZ In Ranlib Namespace, the method of use is also very simple, and the Seed member function can be used to use the Random member function. BLITZ includes random number of various probability distributions, enumerations as follows: uniform, normal distribution, exponential, beta distribution, gamma distribution, χ, F distribution, discrete Evenly distributed. Specifically, you can refer to the ninth chapter of the Blitz document. This article will demonstrate a random number of normally distributed.

NormalUnit <> () Standard normal distribution, μ = 0, σ = 1;

Normal <> (t mean, t standarddeviation) normal distribution, N (μ, σ ^ 2), where Mean is μ, Standarddeviation is σ.

SEEDs in the Blitz are shared. After calling a class, other classes are also in the same seed. For the source of the seed, there is such a precaution in the Blitz document: Note: You May Be Tempted to seed the Random Number Generator from a static initializer. Don't do it! Due to an an oddtem of C , There is no guarance on the order of static initialization when templates are involved Hence, you may seed the RNG before its constructor is invoked, in which case your program will crash If you do not know what a static initializer is, do not worry -.. you ' RE SAFE!

(Fortunately, I am not very clear about what Static Initializer refers to: :-))

1. First look at a standard normal distribution example, according to 3σ law (normal distribution of random number (μ-3σ, μ 3σ)), these random numbers should be mostly (-3) 3) between.

The following program generates 10,000 positive distribution random numbers, and then imports MATLAB generation graphics, the abscissa is the number of random numbers, and the ordinate is a random value. Obviously, most points are in the 3σ interval. The data points most dense in the interval (μ-σ, μ σ).

#include

#include

#include

#include

#include

#include

Using namespace std;

USING NAMESPACE RANLIB;

int main ()

{

Const size_t maxgen = 10000;

NormalUnit rND;

Rnd.seed (Time (0));

OFSTREAM File ("c: //file.txt");

// Generator Normal Distribution Random Number

For (SIZE_T I = 0; I

{

File << rnd.random () << "

}

}

2, below is a normal distribution that obeys μ = 10, σ = 2, according to 3σ law, most points should fall between (4, 16).

#include

#include

#include

#include

#include

#include

Using namespace std;

USING NAMESPACE RANLIB;

int main ()

{

Const size_t maxgen = 1000000;

Const int mu = 10;

Const Int Sigma = 2;

Normal

RND (MU, SIGMA); Rnd.seed (Time (0));

OFSTREAM File ("c: //file.txt");

// Generator Normal Distribution Random Number

For (SIZE_T I = 0; I

{

File << rnd.random () << "

}

}

3. Generate a clock curve (PDF) of the aforementioned state-of-state distribution.

Herein

1M

Float random number, then multiplied by 10 transformation into an integer, statistically in the probability of random number between the interval 0-170, so that the MATLAB generated graphic is a approximate normal distribution clock curve.

#include

#include

#include

#include

#include

#include

Using namespace std;

USING NAMESPACE RANLIB;

int main ()

{

Const size_t maxgen = 1000000;

Const int mu = 10;

Const Int Sigma = 2;

Normal

RND (MU, SIGMA);

Rnd.seed (Time (0));

OFSTREAM File ("c: //file.txt");

Float * rndaRray = new float [maxgen];

// Generator Normal Distribution Random Number

For (SIZE_T I = 0; I

{

RndaRray [i] = rnd.random ();

}

INT * RNDCONVERTINTARRAY = new int [maxgen];

INT MULTIPLE = 10;

// Convert Float Random Number To Integer

For (SIZE_T I = 0; I

{

RNDCONVERTINTARRAY [I] = INT (RndArray [i] * multiple;

}

Const size_t pdfsize = (mu 3 * sigma) * Multiple;

Const size_t redundance = 10;

INT * PDF = New int [pdfsize redundancy];

For (SIZE_T I = 0; i

{

PDF [i] = 0;

}

// generator PDF (Probability Distribution, Normal Distribution IS A "Bell" Shape

For (SIZE_T I = 0; I

{

IF (RNDCONVERTINTARRAY [I] <= pdfsize redundancy-1)

{

PDF [RNDCONVERTITINTARRAY [I]];

}

}

For (size_t i = 0; i

File << PDF [i] << "

}

DELETE [] RNDARRAY;

DELETE [] RNDCONVERTINTARRAY;

delete [] PDF;

}

The clock curve is of course not particularly smooth (j), and most of the random numbers appear in the (4, 16) interval.

Summary, the random number of BLITZ should be trusted.

[references]

1. Probability Theory and Mathematical Statistics (3RD), Zhejiang University Grand Xie Qian Pan Yuyi Companies Higher Education Press

2, C numerical algorithm (2nd), [US] William H. Press, Saul A. Teukolsky, William T. Vetterling,

Brian P. Flannery

Fu Zuzhen Zhao Mei Na Ding Rock, etc., Fu Zulan

3, MATLAB 6.5 Documentation The MathWorks, Inc.

转载请注明原文地址:https://www.9cbs.com/read-116958.html

New Post(0)