| 查看: 835 | 回复: 2 | ||
[求助]
谁给个好点的随机数程序代码啊
|
|
即[0,1]均匀分布的,现有的一个貌似产生的随机数是有关联的,并不“随机”。希望有一个真正随机,或者随机性很好的程序。 现在刚开始学Monte carlo,望高手指点。 |
» 猜你喜欢
计算机、0854电子信息(085401-058412)调剂
已经有5人回复
国自然申请面上模板最新2026版出了吗?
已经有13人回复
基金委咋了?2026年的指南还没有出来?
已经有3人回复
Materials Today Chemistry审稿周期
已经有5人回复
溴的反应液脱色
已经有7人回复
推荐一本书
已经有12人回复
基金申报
已经有4人回复
纳米粒子粒径的测量
已经有7人回复
常年博士招收(双一流,工科)
已经有4人回复
有没有人能给点建议
已经有5人回复
» 本主题相关价值贴推荐,对您同样有帮助:
【在线答疑】经典粒子体系的Monte Carlo 模拟之基础篇
已经有73人回复
yahoohoo
铁杆木虫 (著名写手)
- 模拟EPI: 10
- 应助: 0 (幼儿园)
- 贵宾: 1.55
- 金币: 7632.4
- 散金: 251
- 红花: 4
- 帖子: 1176
- 在线: 167小时
- 虫号: 74894
- 注册: 2005-06-15
- 性别: GG
- 专业: 理论和计算化学
2楼2011-08-26 18:27:45
Bessel
木虫 (正式写手)
- 模拟EPI: 2
- 应助: 47 (小学生)
- 金币: 1506.5
- 散金: 4755
- 红花: 8
- 帖子: 609
- 在线: 234.1小时
- 虫号: 370174
- 注册: 2007-05-13
- 专业: 凝聚态物性I:结构、力学和
【答案】应助回帖
★ ★ ★ ★ ★ ★
ghcacj(金币+6): 谢谢 2011-09-01 15:37:20
ghcacj(金币+6): 谢谢 2011-09-01 15:37:20
|
把这个做头文件,调用RandNumber()就可以了。 #include #include #include #define N 624 #define M 397 #define MATRIX_A 0x9908b0dfUL /* constant vector a */ #define UPPER_MASK 0x80000000UL /* most significant w-r bits */ #define LOWER_MASK 0x7fffffffUL /* least significant r bits */ static unsigned long mt[N]; /* the array for the state vector */ static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */ /* initializes mt[N] with a seed */ void init_genrand(unsigned long s) { mt[0]= s & 0xffffffffUL; for (mti=1; mti (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti); /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ /* In the previous versions, MSBs of the seed affect */ /* only MSBs of the array mt[]. */ /* 2002/01/09 modified by Makoto Matsumoto */ mt[mti] &= 0xffffffffUL; /* for >32 bit machines */ } } /* initialize by an array with array-length */ /* init_key is the array for initializing keys */ /* key_length is its length */ /* slight change for C++, 2004/2/26 */ void init_by_array(unsigned long init_key[], int key_length) { int i, j, k; init_genrand(19650218UL); i=1; j=0; k = (N>key_length ? N : key_length); for (; k; k--) { mt = (mt ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL)) + init_key[j] + j; /* non linear */ mt &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ i++; j++; if (i>=N) { mt[0] = mt[N-1]; i=1; } if (j>=key_length) j=0; } for (k=N-1; k; k--) { mt = (mt ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL)) - i; /* non linear */ mt &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ i++; if (i>=N) { mt[0] = mt[N-1]; i=1; } } mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ } /* generates a random number on [0,0xffffffff]-interval */ unsigned long genrand_int32(void) { unsigned long y; static unsigned long mag01[2]={0x0UL, MATRIX_A}; /* mag01[x] = x * MATRIX_A for x=0,1 */ if (mti >= N) { /* generate N words at one time */ int kk; if (mti == N+1) /* if init_genrand() has not been called, */ init_genrand(5489UL); /* a default initial seed is used */ for (kk=0;kk mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL]; } for (;kk mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL]; } y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK); mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL]; mti = 0; } y = mt[mti++]; /* Tempering */ y ^= (y >> 11); y ^= (y << 7) & 0x9d2c5680UL; y ^= (y << 15) & 0xefc60000UL; y ^= (y >> 18); return y; } /* generates a random number on [0,1]-real-interval */ double RandomNumber(void) { return genrand_int32()*(1.0/4294967295.0); /* divided by 2^32-1 */ } |
3楼2011-09-01 10:11:58











回复此楼