Gauss random number

zhaozj2021-02-16  49

Attached 16 bits, 32BITS uniform random number generating program procedure only produces a random number of 0, the variance is 1, the random number of the mean E, the variance is D, as long as the random number * D E is OK. The Gaussian random number is still with parameters, and the parameter is used to describe a floating point table table for normal distribution. When executing the program, gently make this table (only once), and then perform the Gaussian random number generating program multiple times. Using C is to ensure universality, if someone feels trouble, you can use C to make a class, and package these things into it. In addition, if someone is interested, it can modify it into any form of continuous random number generating programs, and the modification is very simple, this is not prompted here.

#include #include #include #define GAUSS_TABLE_LENGTH 256 # include "random.h" extern float Gauss_F [GAUSS_TABLE_LENGTH]; extern float Gauss_f [GAUSS_TABLE_LENGTH]; extern DWORD RandomKey, RandomKey32; DWORD MCoef_32 [2] = {0xE7BD2160,0xDA3A2A9C}; WORD MCode_16 [4] = {0xD31C, 0xC36C, 0xD0A2,0xD228}; // two m sequence # define INVALID_BLOCK 0xFFFF # define BLOCK_SIZE 512DWORD m_Seq_32 (DWORD Key) {int i; for (i = 0; i <32; i ) {_ asm {mov ebx, key; shl EBX, 1MOV EAX, Key; Mov Edx, MCoEf_32 [0]; and Eax, edx; // select the bit for xormov EDX, EAX; SHR EAX, 16; XOR AX; XOR AH, Al; // Because P Only Judge One Byte // So Must XOR To Judge To WHOLALS The XORINC EBXNEXT: MOV KEY , EBX;}}} word rng () {DWORD A, B; _ASM {_emit 0x0f_emit 0x31MOV A, Eaxmov B, EDX} randomkey = m_seq_32 (Randomkey ^ a ^ b); return (word) randomkey;} dWord RNG32 () {DWORD A, B; _ASM {_emit 0x0f_emit 0x31mov A, Eaxmov B, EDX} randomkey32 = m_seq_32 (Randomkey32 ^ A ^ b); returnitomkey32;} void NormalTable (Float * fTable, float * f Table, intlene) / * Generate a normal distribution function and a normal distribution probability density function table, the table width is 3 times the difference between 0, the variance is 1ftable: normal distribution function table fTable: normal distribution probability Density table Length: Table length * / {INT I; float h; / * step length * / float x, temp; float c; x = -3; h = 6.0 / length / 2; ftable [0] = 0; C = 1 / SQRT (2 * 3.1415927); / * Initial parameter setting * / ftable [0] = exp (-x * x / 2) * c; / * Probability density, exp (-x * x / 2) * c is a probability density function * / for (i = 1; i

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

New Post(0)