问题描述
我想设计一个彩票调度,我需要有一个很好的(伪)随机数类似于LCG发电机,但如果有其他更好的选择,在那里与否我想知道?我专门找C语言编写的随机生成。
I want to design a lottery scheduler and I need to have a very good (pseudo) random number generator similar to LCG but I was wondering if there are other better choice out there or not? I am specifically looking for random generators written in C.
LCG code:
unsigned long lcg_rand(unsigned long a)
{
return (a * 279470273UL) % 4294967291UL;
}
此外,我想知道,如果函数srand()
可用于此目的或者不是非常准确?
Also I would like to know if srand()
can be used for this purpose or is not highly accurate?
推荐答案
如果您需要简单,但体面的质量,我会应用到回火功能,使用64位LCG的高32(或更少)位,可能是输出。做这件事时,我已经复制在使用的回火功能。我不会真的建议使用梅森难题,因为它有很多比其他的PRNG的复杂性和内部状态,而不会显著更好的品质。
If you need simple but decent quality, I would use the upper 32 (or fewer) bits of a 64-bit LCG, possibly with a tempering function applied to the output. When doing this, I've copied the tempering function used in Mersenne Twister. I would not recommend actually using Mersenne Twister, as it has a lot more complexity and internal state than other PRNGs without significantly better qualities.
下面是一些示例code:
Here is some sample code:
static uint32_t temper(uint32_t x)
{
x ^= x>>11;
x ^= x<<7 & 0x9D2C5680;
x ^= x<<15 & 0xEFC60000;
x ^= x>>18;
return x;
}
uint32_t lcg64_temper(uint64_t *seed)
{
*seed = 6364136223846793005ULL * *seed + 1;
return temper(*seed >> 32);
}
这篇关于什么是更好的(伪)随机数比LCG彩票调度发电机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!