Normal distribution random number generator in C #

xiaoxiao2021-03-06  68

The "Numeric Recipes IN C 2 / E" P.292 ~ P.294 and "Simulation Modeling and Analysis 3 / E" P. 465 ~ P.466.

Box and Muller give algorithms that generate normal distributions from a uniform distribution of random variables in 1958. Set U1, U2 is a random variable that is evenly distributed on the interval (0, 1), and is independent of each other. Let X1 = SQRT (-2 * log (u1)) * COS (2 * pi * u2); x2 = sqrt (-2 * log (u1)) * sin (2 * pi * u2); then x1, x2 service N (0, 1) distribution and independent of each other. To say that we have used two independent U (0,1) random numbers to obtain two independent N (0, 1) random numbers. Marsaglia and Bray proposed a improved algorithm in 1964 to avoid using triangle functions. The following implementation of the code is this improvement algorithm. //////////////////////////////////////////////////////////////////////////////////////////////////////// / e '', P.293 ~ P.294 // Public Class Gaussianrng {Int ISET; Double Gset; Random R1, R2; PUBLIC Gaussianrng () {r1 = new random (unchecked (int) DateTime.now.ticks)); r2 = new random (~ unchecked ((int) DateTime.now.ticks)); ISET = 0;} public Double Next ) {DOUBLE FAC, RSQ, V1, V2; IF (ISET == 0) {DO {V1 = 2.0 * r1.nextdouble () - 1.0; v2 = 2.0 * r2.nextdouble () - 1.0; rsq = v1 * v1 v2 * v2;} while (RSQ> = 1.0 || rsq == 0.0); FAC = Math.SQRT (-2.0 * math.log (rsq) / RSQ); gset = v1 * fac; ISET = 1; Return V2 * FAC;} else {iset = 0; return gset;}}}

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

New Post(0)