当前位置: 首页 > article >正文

FPGA生成随机数的方法

FPGA生成随机数的方法,目前有以下几种:

1、震荡采样法

        实现方式一:通过低频时钟作为D触发器的时钟输入端,高频时钟作为D触发器的数据输入端,使用高频采样低频,利用亚稳态输出随机数。

        实现方式二:使用三个或以上反相器首尾互联,形成环路振荡器。使用时钟采集,得到不稳定的数据,以形成随机数(其实此种方式与方式一相类似,反相器的组合逻辑相当于高频时钟,时钟信号相当于低频时钟,同样利用亚稳态生成随机数)。

(直接按照实现方式二,上具体实现代码)


//反相器环形级联,组合为振荡器,利用亚稳态状态,得到随机数module random_gen
(input clk,input start, //开始命令output reg random_valid = 0,output reg [7:0] random_data = 0);reg state = 1'b0; //0空闲态  1随机态(* ALLOW_COMBINATORIAL_LOOPS = "true" *)(*dont_touch = "true" *)reg a = 1'b0;
(* ALLOW_COMBINATORIAL_LOOPS = "true" *)(*dont_touch = "true" *)reg b = 1'b0;
(* ALLOW_COMBINATORIAL_LOOPS = "true" *)(*dont_touch = "true" *)reg c = 1'b0;reg [2:0] bit_cnt = 0; //3个取反器,环形震荡
//取反器
always@(state)
beginb <= !a;
end//取反器
always@(state)
beginc <= !b;
end//取反器
always@(state)
begina <= !c;
endalways@(posedge clk)
beginif(state == 1'b0) begin  //state == 0bit_cnt <= 3'd0;random_valid <= 1'b0;if(start) begin state <= 1'b1;endelse beginstate <= 1'b0;endendelse begin   //state == 1bit_cnt <= bit_cnt + 1'b1;if(bit_cnt == 3'b111) beginstate <= 1'b1;random_valid <= 1'b1;endelse beginrandom_valid <= 1'b0;      endrandom_data <= {random_data[6:0],c};end
end       endmodule

针对于

(* ALLOW_COMBINATORIAL_LOOPS = "true" *)约束,也可以再约束文件中增加约束:


#采用组合逻辑形式的环形震荡生成随机数,因此进行约束
set_property ALLOW_COMBINATORIAL_LOOPS true [get_nets random_gen/a]
set_property ALLOW_COMBINATORIAL_LOOPS true [get_nets random_gen/b]
set_property ALLOW_COMBINATORIAL_LOOPS true [get_nets random_gen/c]


2、LFSR伪随机数

        LFSR(Linear-feedback shift register)是一种特殊的的移位寄存器,他的输入取决于其先前状态,其生成的随机数是一种伪随机数。其架构如下图所示:

        上图中,gn为反馈系数,取值只能是1或者0,N个D触发器,可以提供2^(N-1)个输出状态,为了保证随机性,gn的选择必须满足一定的条件,不能全部为0,且gn必须等于1。

        假设输出的随机数的位宽为8bits,且取值g0g1g2g3g4g5g6g7g8=101110001,则可以实现如下代码:

module LFSR #
( parameter [7:0] SEED = 8'h11) 
(input               rstn,    input               clk,       output reg [7:0]    rand_data //随机数
);always@(posedge clk or negedge rstn)
beginif(!rstn)rand_data    <=SEED;else beginrand_data[0] <= rand_data[7];rand_data[1] <= rand_data[0];rand_data[2] <= rand_data[1];rand_data[3] <= rand_data[2];rand_data[4] <= rand_data[3]^rand_data[7];rand_data[5] <= rand_data[4]^rand_data[7];rand_data[6] <= rand_data[5]^rand_data[7];rand_data[7] <= rand_data[6];end       
endendmodule


3、使用FLASH或EEPROM生成随机数

        读取FLASH中预存的seed,然后进行相关逻辑运算,得到伪随机数。同时按照既定的逻辑生成新的seed,写入到FLASH或者EEPROM中,这样就可以保证每次输出的随机数不同。(此种方式较为复杂,不推荐)

        此处不进行具体实现。

http://www.lryc.cn/news/2378861.html

相关文章:

  • 【Linux C/C++开发】轻量级关系型数据库SQLite开发(包含性能测试代码)
  • 2025认证杯第二阶段数学建模B题:谣言在社交网络上的传播思路+模型+代码
  • 记录算法笔记(2025.5.17)验证二叉搜索树
  • flutter编译时 设置jdk版本
  • ctfshow——web入门254~258
  • 【数据处理】xarray 数据处理教程:从入门到精通
  • qt5.14.2 opencv调用摄像头显示在label
  • 科技的成就(六十八)
  • 芯片生态链深度解析(三):芯片设计篇——数字文明的造物主战争
  • Rocky Linux 9.5 基于kubeadm部署k8s
  • --openssl-legacy-provider is not allowed in NODE_OPTIONS 报错的处理方式
  • uart16550详细说明
  • deepin v23.1 音量自动静音问题解决
  • 抢跑「中央计算+区域控制」市场,芯驰科技高端智控MCU“芯”升级
  • 《算法导论(第4版)》阅读笔记:p82-p82
  • day015-进程管理
  • traceroute命令: -g与-i 参数
  • POWER BI添加自定义字体
  • SpringAI更新:废弃tools方法、正式支持DeepSeek!
  • 协议不兼容?Profinet转Modbus TCP网关让恒压供水系统通信0障碍
  • ChatGPT + DeepSeek 联合润色的 Prompt 模板指令合集,用来润色SCI论文太香了!
  • 全栈项目搭建指南:Nuxt.js + Node.js + MongoDB
  • RAGFlow Arbitrary Account Takeover Vulnerability
  • Python 之 Flask 入门学习
  • 微服务,服务粒度多少合适
  • 【Ragflow】22.RagflowPlus(v0.3.0):用户会话管理/文件类型拓展/诸多优化更新
  • 使用PocketFlow构建Web Search Agent
  • 安卓基础(Bitmap)
  • 记录:echarts实现tooltip的某个数据常显和恢复
  • 八股文--JVM(1)