rand() 的实现 [英] implementation of rand()

查看:26
本文介绍了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?

它用于图像处理,因此相对高质量"意味着合适的循环长度和良好的均匀特性.

It's for image processing, so "relatively high quality" means decent cycle length and good uniform properties.

推荐答案

看看这个 George Marsaglia 的随机数生成器集合.他是随机数生成方面的领先专家,所以我有信心使用他推荐的任何东西.该列表中的生成器很小,有些只需要几个 unsigned long 作为状态.

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 的发电机以您的长期和良好均匀分布的标准来看绝对是高质量"的.它们通过了严格的统计测试,尽管它们不会用于密码学.

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.

这篇关于rand() 的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆