[NUCLEO-L476RG开发] 随机数
随便玩玩{:3_52:}数据全放ram,add watch 不输出!.
均值什么的随便算算..
union floathex
{
unsigned int hexvalue;
float floatvalue;
};
floathex rand;
double sum;
double average;
double var;
double _var;
初始化:
void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng)
{
__HAL_RCC_RNG_CLK_ENABLE();
}
__________________________________________
// IN MAIN()
HAL_Init();
SystemClock_Config();
hrng.Instance = RNG;
__HAL_RCC_RNG_CONFIG(RCC_RNGCLKSOURCE_MSI) ;
HAL_RNG_Init(&hrng);
//SET_BIT(RNG->CR,RNG_CR_RNGEN);
state=HAL_RNG_GenerateRandomNumber(&hrng,&rand.hexvalue);
state=HAL_RNG_GenerateRandomNumber(&hrng,&rand.hexvalue);
state=HAL_RNG_GenerateRandomNumber(&hrng,&rand.hexvalue);
state=HAL_RNG_GenerateRandomNumber(&hrng,&rand.hexvalue);看上去似乎是可以的..
然后生成随机数(0,1)来简单看看.
虽然我学识浅薄,但品均值大概在0.5左右还是知道的...
while(1)
{
int i;
for(i=0;i<20000;i++)
{
state=HAL_RNG_GenerateRandomNumber(&hrng,&rand.hexvalue);
rand.hexvalue =(rand.hexvalue>>9) |0x3f800000;
rand.floatvalue -= 1.0f;
}结果略:(关虚拟机后才想起没截图...)
Anyway ,
明天又周末了{:3_48:}
这个笔记有点凑合哦,楼主加油,相信有更好的内容;P 楼主辛苦,谢谢 学习学习 我只看看不说话 软件也可以哦,一个函数rand()搞定~:lol
页:
[1]