[转] A sample of Multiply-with-carry, a pseudo-random number generator
2013-01-25 10:30
816 查看
The following is an implementation of the CMWC algorithm in the C programming language. Also, included in the program is a sample initialization function. In this implementation the lag r=4096. The period of the resulting generator is about
![](http://upload.wikimedia.org/math/2/e/1/2e1b20d811c91f4187ece1e82dbd20b7.png)
.
Wiki Multiply-with-carry
More: RNG in CryptoSys
![](http://upload.wikimedia.org/math/2/e/1/2e1b20d811c91f4187ece1e82dbd20b7.png)
.
#include <stdint.h> #define PHI 0x9e3779b9 static uint32_t Q[4096], c = 362436; void init_rand(uint32_t x) { int i; Q[0] = x; Q[1] = x + PHI; Q[2] = x + PHI + PHI; for (i = 3; i < 4096; i++) Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i; } uint32_t rand_cmwc(void) { uint64_t t, a = 18782LL; static uint32_t i = 4095; uint32_t x, r = 0xfffffffe; i = (i + 1) & 4095; t = a * Q[i] + c; c = (t >> 32); x = t + c; if (x < c) { x++; c++; } return (Q[i] = r - x); }
Wiki Multiply-with-carry
More: RNG in CryptoSys
相关文章推荐
- Q138:PBRT-V3,伪随机数发生器(pseudo-random number generator,RNG)(A.1.2章节)
- 可变参数宏的一种实现:(Macros with a Variable Number of Arguments)
- Codeforces Beta Round #27 E. Number With The Given Amount Of Divisors 含n个约数最小数
- codeforces 27E Number With The Given Amount Of Divisors(反素数)
- CodeForces 27 E.Number With The Given Amount Of Divisors(dfs)
- tomcat 大并发报错 Maximum number of threads (200) created for connector with address null and port 8080
- CareerCup how will you test if the random number generator is generating actual random numbers
- Kettle解析JSON错误,We MUST have the same number of values for all paths,We can not find and data with path [$.
- Find next greater number with same set of digits
- Codeforces Beta Round #27 E. Number With The Given Amount Of Divisors 含n个约数最小数
- Number of permutation with K inversions
- codeforces 27 E. Number With The Given Amount Of Divisors(反素数)
- Find number of subsets with equal sum
- inserting a large number of records with SQLiteStatement.
- [LeetCode] Next Permutation - Next bigger number with the same set of digits
- 67. 总结篇:面试中随机数"等概率"vs"不等概率"生成问题[random generator with equal or unequal probability]
- CodeForces - 27E Number With The Given Amount Of Divisors (DFS+数学)
- CF27E:Number With The Given Amount Of Divisors(反素数)
- 解决:Maximum number of threads (200) created for connector with address null
- Macros with a Variable Number of Arguments.