用单片机生成随机数算法,伪随机,使用姚老板的12c5a60s2芯片,详情见文档
输入[0,65536)
输出[0,65536)
-
-
-
U16 rand65536( CU16 seed )/*2018.04.13Python+excel验证*/
{
code U8 table[256] =
{
108,166,74,119,198,161,224,60,106,227,26,20,44,55,172,91,69,194,70,125,249,188,113,8,155,114,
205,37,63,47,57,217,177,71,244,65,52,61,132,123,196,242,43,73,12,187,247,151,184,81,90,255,
175,51,143,59,239,27,186,7,2,46,24,131,109,96,203,42,218,146,174,72,110,102,160,169,222,56,
207,13,180,250,116,67,171,84,100,9,137,6,129,141,154,240,122,202,219,163,130,216,75,135,221,98,
157,88,226,253,33,101,103,138,204,121,127,193,89,85,62,94,139,86,34,115,168,152,142,16,126,170,
120,15,76,10,147,29,183,4,148,40,66,32,87,105,124,214,162,140,234,235,68,104,220,92,167,212,
233,14,248,78,210,144,236,112,209,201,19,93,97,182,58,118,153,38,211,50,192,45,35,254,165,
0,229,237,25,208,1,23,17,158,21,95,195,164,223,243,48,199,181,145,53,228,83,190,117,5,
173,54,197,18,80,231,159,200,213,252,128,215,31,150,49,64,251,179,3,82,28,206,230,246,225,
11,36,149,136,245,22,232,30,238,185,191,176,241,77,39,156,41,99,189,134,79,133,178,111,107
};
auto xdata U16 t1, t2;
t1 = seed / 256;
t2 = seed % 256;
return( table[( t1 + t2 ) % 256 ] * 256 + table[ 255 - t2 ] );
}
是啊如果你这个数组是ADC引脚上的波动,那就差不多真是随机了.
是的,adc波动是个常规方法
问题是好的adc能做到dc下95%恒定率,其余5%也就+-1。。。
//随机数初始化
void init_randSeed(unsigned int *seeds,int len)
{
__IO unsigned int seed=0;
int i;
for(i=0; i<len; i++)
{
seed|=(seeds[i]&0x01);
seed<<=1;
}
srand(seed);
}
void init_adc()
{
__IO float Temper;
unsigned int uhADCxConvertedValue[32];
int i;
for(i=0; i<32; i++)
{
if(HAL_ADC_Start(&hadc1) == HAL_OK)
{
HAL_ADC_PollForConversion(&hadc1, 30);
uhADCxConvertedValue[i] = HAL_ADC_GetValue(&hadc1);
}
//温度
Temper = (uhADCxConvertedValue[i]*3300/4096-760)/2.5 + 25;
}
init_randSeed(uhADCxConvertedValue,32);
}
2、随机种子一般可以从按键触发时间,ad随机采样,两个独立时钟差值产生。
比如可以用两个时钟源,最好是悬殊较大的时钟源,各自计数,随机数就直接从差值里面取,范围直接就是低几位
一周热门 更多>