当前位置:网站首页>单片机没有随机数发生器如何生成随机数——2022.07.26
单片机没有随机数发生器如何生成随机数——2022.07.26
2022-07-30 02:21:00 【ZZZ_XXJ】
有时项目中需要用到随机数,但并不是每种单片机都有真随机数发生器,如何在没有随机数发生器的单片机中生成随机数呢?
这里分享两种单片机产生随机数的方法,这些也是我在项目中经常会用到的。
方法一:C库函数 rand()
最方便快捷也是最经典的方法就是直接使用C库函数 rand() 来生成随机数。
extern _ARMABI int rand(void);
/* * Computes a sequence of pseudo-random integers in the range 0 to RAND_MAX. * Uses an additive generator (Mitchell & Moore) of the form: * Xn = (X[n-24] + X[n-55]) MOD 2^31 * This is described in section 3.2.2 of Knuth, vol 2. It's period is * in excess of 2^55 and its randomness properties, though unproven, are * conjectured to be good. Empirical testing since 1958 has shown no flaws. * Returns: a pseudo-random integer. */
使用方法:
rand()函数的使用方法就不贴上来了,这个是C语言的基础函数之一,大家肯定都会的。
方法二:利用ADC噪声
由于热噪声和生产误差等因素,ADC原始数据的最低1位总会有波动,我们可以利用这些波动生成随机数。
拿stm32来举例,可以利用stm32测内核温度的ADC通道来生成随机数。
生成代码如下,ADC初始化部分的就不放上来了,就是普通的DMA模式采集数据的ADC初始化,打开ADC1的通道16。利用DMA连续转换32个ADC原始数据,取每个数据的最低位依次拼接成一个32bit随机数。
uint32_t adc_randnum;
uint16_t adc_data[32];
void DMA1_Channel1_IRQHandler(void)
{
static __IO uint8_t count = 0;
if(DMA_GetITStatus(DMA1_INT_TC1) == SET)
{
DMA_ClearFlag(DMA1_INT_TC1);
ADC_DMACtrl(ADC1, DISABLE);
ADC_Ctrl(ADC1, DISABLE);
for(uint32_t i = 0; i < 32; i += 4)
{
adc_randnum += (adc_data[i] & 0x0001) << i;
adc_randnum += (adc_data[i+1] & 0x0001) << i;
adc_randnum += (adc_data[i+2] & 0x0001) << i;
adc_randnum += (adc_data[i+3] & 0x0001) << i;
}
ADC_DMACtrl(ADC1, ENABLE);
ADC_Ctrl(ADC1, ENABLE);
ADC_SoftwareStartConvCtrl(ADC1, ENABLE);
}
}
边栏推荐
- Kotlin接口
- mysql 报错 is too long for user name (should be no longer than 16)
- 综合设计一个OPPO页面--返回顶部使用--使用链接的锚点a+id
- 【内部资源】冲击年薪30W+的软件测试人员,这份资料必须领取
- org.apache.ibatis.binding.BindingException Invalidbound statement (not found)的解决方案和造成原因分析(超详细)
- JS history.back() go(-1) Location 跳转 重新加载页面 get请求 返回顶部 bom
- postgresql日常运维技能,适合初学者
- JS develops 3D modeling software
- LeetCode 2342. Digital and equal number of one of the biggest and
- tcp ip
猜你喜欢

nrm ls 为什么前面不带 *了

利用ESP32构造一个ZIGBEE的网络发送转接

API interface batch test

SwiftUI SQLite Database Storage Tutorial Collection (2022 Edition)
![[Notes] Stuttering word segmentation to draw word cloud map](/img/a1/05504ad82d4670386d1cc233291c6a.png)
[Notes] Stuttering word segmentation to draw word cloud map

戴尔首款纯软产品,再定义下一代对象存储

SwiftUI SQLite数据库存储使用教程大合集(2022年版)

JS develops 3D modeling software

LeetCode 2342. Digital and equal number of one of the biggest and

Type-C边充电边OTG芯片——LDR6028A
随机推荐
el-table加合计
mysql 报错 is too long for user name (should be no longer than 16)
绘制概率密度图
重写并自定义依赖的原生的Bean方法
再度入围|“国产化”大潮来袭,汉得助力乘风破浪!
el-table sum total
复旦-华盛顿大学EMBA科创的奥E丨《神奇的材料》与被塑造的我们
力扣刷题训练(二)
SwiftUI SQLite Database Storage Tutorial Collection (2022 Edition)
Postgresql daily operation and maintenance skills, suitable for beginners
LeetCode 2348. Number of all-zero subarrays
可惜了!规模这么大的上市公司说散就散了
postgresql日常运维技能,适合初学者
STM32L4R9ZIY6PTR STM32L4 high-performance embedded-MCU
超详细的MySQL基本操作
matlab洗碗机节水模型的优化设计-这是个课题名称,不是买洗碗机,审核的人仔细看下,谢谢
黑客动态播报 | 一封假offer,盗取6.25亿美元
ESP8266 +0.96“ I2C OLED 表盘时钟
HCIP 第十四天
go grpc custom interceptor