rand() 的实现
我正在用 C 编写一些嵌入式代码,需要使用 rand() 函数。 不幸的是,控制器的库不支持 rand()。 我需要一个快速的简单实现,但更重要的是空间开销很小,可以产生相对高质量的随机数。 有谁知道使用哪种算法或示例代码?
编辑:它用于图像处理,因此“相对高质量”意味着良好的周期长度和良好的均匀特性。
I am writing some embedded code in C and need to use the rand() function. Unfortunately, rand() is not supported in the library for the controller. I need a simple implementation that is fast, but more importantly has little space overhead, that produces relatively high-quality random numbers. Does anyone know which algorithm to use or sample code?
EDIT: It's for image processing, so "relatively high quality" means decent cycle length and good uniform properties.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
查看 George Marsaglia 的随机数生成器集合。 他是随机数生成方面的领先专家,因此我有信心使用他推荐的任何内容。 该列表中的生成器很小,有些只需要几个无符号长整型作为状态。
按照长期使用和良好均匀分布的标准,Marsaglia 的发电机绝对是“高品质”的。 它们通过了严格的统计测试,尽管它们不适用于密码学。
Check out this collection of random number generators from George Marsaglia. He's a leading expert in random number generation, so I'd be confident using anything he recommends. The generators in that list are tiny, some requiring only a couple unsigned longs as state.
Marsaglia's generators are definitely "high quality" by your standards of long period and good uniform distribution. They pass stringent statistical tests, though they wouldn't do for cryptography.
使用C 代码作为来自 L'écuyer 的 LFSR113:
非常高的质量和速度。 不要将 rand() 用于任何事情。
这比无用更糟糕。
Use the C code for LFSR113 from L'écuyer:
Very high quality and fast. Do NOT use rand() for anything.
It is worse than useless.
这是 ANSI C 一些随机数生成器的实现的链接。
Here is a link to a ANSI C implementation of a few random number generators.
我制作了一系列随机数生成器“simplerandom”,它们结构紧凑,适合嵌入式系统。 该集合可在 C 和 Python。
我四处寻找一些我能找到的简单而体面的东西,并将它们放在一个小包中。 它们包括几个 Marsaglia 生成器(KISS、MWC、SHR3)和几个 L'Ecuyer LFSR 生成器。
所有生成器都返回一个无符号 32 位整数,并且通常具有由 1 到 4 个 32 位无符号整数组成的状态。
有趣的是,我发现了 Marsaglia 生成器的一些问题,并且我尝试修复/改进所有这些问题。 这些问题是:
我发现了一些有关播种的问题,并尝试制定强大的播种(初始化)程序,因此如果您给它们一个“坏”种子值,它们就不会中断。
I've made a collection of random number generators, "simplerandom", that are compact and suitable for embedded systems. The collection is available in C and Python.
I've looked around for a bunch of simple and decent ones I could find, and put them together in a small package. They include several Marsaglia generators (KISS, MWC, SHR3), and a couple of L'Ecuyer LFSR ones.
All the generators return an unsigned 32-bit integer, and typically have a state made of 1 to 4 32-bit unsigned integers.
Interestingly, I found a few issues with the Marsaglia generators, and I've tried to fix/improve all those issues. Those issues were:
I uncovered a few issues with seeding, and tried to make robust seeding (initialisation) procedures, so they won't break if you give them a "bad" seed value.
我推荐学术论文最小标准随机数生成器的两种快速实现大卫·卡尔塔. 您可以通过 Google 找到免费的 PDF。 关于最小标准随机数生成器的原始论文也值得一读。
Carta 的代码在 32 位机器上提供快速、高质量的随机数。 如需更全面的评估,请参阅论文。
I recommend the academic paper Two Fast Implementations of the Minimal Standard Random Number Generator by David Carta. You can find free PDF through Google. The original paper on the Minimal Standard Random Number Generator is also worth reading.
Carta's code gives fast, high-quality random numbers on 32-bit machines. For a more thorough evaluation, see the paper.
梅森扭曲器
来自维基百科的一点信息:
它通过了许多统计随机性测试,包括 Diehard 测试。 它通过了大多数(但不是全部)更严格的 TestU01 Crush 随机性测试。
链接上提供了多种语言的源代码。
Mersenne twister
A bit from Wikipedia:
It passes numerous tests for statistical randomness, including the Diehard tests. It passes most, but not all, of the even more stringent TestU01 Crush randomness tests.
source code for many languages available on the link.
我会从 GNU C 库中获取一个,源代码可以在线浏览。
http://qa.coreboot.org/docs/libpayload/rand_8c-source。 但是,
如果您对随机数的质量有任何担忧,您可能应该查看更仔细编写的数学库。 这是一个很大的主题,专家们并没有高度重视标准的 rand 实现。
这是另一种可能性: http://www.boost.org/ doc/libs/1_39_0/libs/random/index.html
(如果您发现有太多选项,您可以随时随机选择一个。)
I'd take one from the GNU C library, the source is available to browse online.
http://qa.coreboot.org/docs/libpayload/rand_8c-source.html
But if you have any concern at all about the quality of the random numbers, you should probably look at more carefully written mathematically libraries. It's a big subject and the standard
rand
implementations aren't highly thought of by experts.Here's another possibility: http://www.boost.org/doc/libs/1_39_0/libs/random/index.html
(If you find you have too many options, you could always pick one at random.)
我发现了这个:简单随机数生成,作者:John D.做饭。
鉴于它只有几行代码,应该很容易适应 C。
编辑:您可以澄清“相对高质量”的含义。 您是否正在为核发射代码生成加密密钥,或者为扑克游戏生成随机数?
I found this: Simple Random Number Generation, by John D. Cook.
It should be easy to adapt to C, given that it's only a few lines of code.
Edit: and you could clarify what you mean by "relatively high-quality". Are you generating encryption keys for nuclear launch codes, or random numbers for a game of poker?
更好的是,使用多个线性反馈移位寄存器将它们组合在一起。
假设
sizeof(unsigned) == 4
:Better yet, use multiple linear feedback shift registers combine them together.
Assuming that
sizeof(unsigned) == 4
:标准解决方案是使用线性反馈移位寄存器。
The standard solution is to use a linear feedback shift register.
有一个简单的RNG,名为KISS,它是根据三个数字生成一个随机数生成器。
还有一个测试 RNG 的网站 http://www.phy.duke .edu/~rgb/General/dieharder.php
There is one simple RNG named KISS, it is one random number generator according to three numbers.
Also there is one web site to test RNG http://www.phy.duke.edu/~rgb/General/dieharder.php