我正在与我的 friend 一起构建 ATmega128 研究项目,我们遇到了随机数生成器(从 0 到 5)的问题,因为函数总是显示相同的结果。我们不能添加 time.h,因为 AVR Studio 不接受它。
代码如下:
uint8_t randomNumber(uint8_t r){
r = rand()%5;
return r;
}
其他尝试
uint8_t randomNumber(uint8_t min, uint8_t max){
uint8_t = result;
result = min + rand() % (max+1 - min);
return result;
}
有什么想法吗?谢谢,塞巴斯蒂安
哇,这个问题让我掉进了兔子洞。
- 伪随机数相对容易生成。
- 真正的随机数很难生成。
- 随机数的质量(其中是否出现偏差)完全取决于您的种子值。
- 随机数生成器的种子值必须是(等等)随机的,否则人们可以猜测您使用的是哪个数字,从而破坏生成器的随机性。
从哪里获得随机种子值?
互联网提出的选项:
- 来自环境的自然噪音(阅读 adc,或... https://www.fourmilab.ch/hotbits/(我知道这对于 arduino 项目不实用,但仍然很有趣))。
- 为用户输入计时(人类默认情况下并不精确)。
- 晶体之间的时间差异。 [ https://en.wikipedia.org/wiki/Clock_drift]
温和的免责声明:1/3 已被证明在商业环境中是不安全的,很容易看出 #2 是如何通过计算机而不是人类来进行游戏的。
所以最快的方法可能是使用 float ADC。在您认为这是个好主意之前:https://skemman.is/bitstream/1946/10689/1/ardrand.pdf
请记住:较大的种子池会增加随机性(也就是使用 32 位随机种子值比使用 bool 随机种子值更好)。
128 上的 ADC 有 1024 个值,实际上,浮点值趋向于远小于这个值(我读过你应该把它当作 32)。
要提高获得随机数的机会,请多次从 adc 读取中获取最低位(也就是读取 adc 16 次以获得 16 位“随机”数)。
假设您设置了 adc 等。
未经测试的伪代码
/* srand example */
#include <stdio.h> /* printf, NULL */
#include <stdlib.h> /* srand, rand */
#include <avr/io.h>
//pseudo code. you must implement init_adc() and read_adc()
int main ()
{
//Init and seed.
uint16_t u_rand_val = 0;
uint16_t u_seed_rand_val = 0;
init_adc();
//Note we're assuming the channel that you are reading from is FLOATING or hooked up to something very noisy.
//Gather bits from the adc, pushing them into your pseudorandom seed.
for(uint8_t i=0; i<16; i++){
u_seed_rand_val = u_seed_rand_val<<1 | (read_adc()&0b1);
}
srand (u_seed_rand_val);
while(1){
//Do whatever you were going to do.
//Note that calls to rand() use the seed set up by srand above.
u_rand_val = rand()%5;
print("Cur val:%u", u_rand_val);
}
return 0;
}
我是一名优秀的程序员,十分优秀!