Random number generation class C ++ source code

xiaoxiao2021-03-06  85

#include #include

Using namespace std; class randomnumber {public: randomnumber (long s = 0); long random (long n); double frandom ();

PRIVATE: STATIC Const long A; static constling q; static constlong r;

Long seed;

Const long randomnumber :: a = 48271; const line randomnumber :: m = 2147483647; const line randomnumber :: q = m / a; const line randomnumber :: r = m% a;

Randomnumber :: randomnumber (long s) {if (s <0) s = 0; if (s == 0) {long t_time = Time (null); t_time * = t_time; if (t_time> 0) s = t_time ^ 0x5eece66dl; Else if (t_time <0) s = (t_time & 0x7ffffff) ^ 0x5eece66dl; Else S = 0x5eece66dl;} seed = s;}

Long randomnumber :: random () {long tmpseed = a * (seed% q) - r * (seed / q);

IF (tmpseed> = 0) SEED = TmpSeed; else seed = tmpSeed m;

Return seed;

Long randomnumber :: random (long n) {double foot = double (random ()) / double (m);

Return Int (FRAction * N);

Double Randomnumber :: Frandom () {Return Double (Random ()) / Double (M);

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

New Post(0)