vhdl中随机整数的生成

vhdl中随机整数的生成,vhdl,fpga,Vhdl,Fpga,我需要在vhdl中生成一个介于0-1023之间的随机整数,但是我在internet上找不到一个好的资源。有人能帮我吗?下面是一个生成均匀(偶数)分布范围为[0:1023]的整数的示例 请注意,floor操作必须在与最大值+1相乘后使用,在这种情况下,1023+1=1024,以确保[0:1023]中所有整数值的均匀(均匀)分布,因为在示例中使用了无下限整数(因此integer(x*1023))根据注释“在VHDL测试台中生成随机数”将导致四舍五入(从一半向上),因此该范围内第一个和最后一个值(0和

我需要在vhdl中生成一个介于0-1023之间的随机整数,但是我在internet上找不到一个好的资源。有人能帮我吗?

下面是一个生成均匀(偶数)分布范围为[0:1023]的整数的示例

请注意,
floor
操作必须在与最大值+1相乘后使用,在这种情况下,1023+1=1024,以确保[0:1023]中所有整数值的均匀(均匀)分布,因为在示例中使用了无下限整数(因此
integer(x*1023)
)根据注释“在VHDL测试台中生成随机数”将导致四舍五入(从一半向上),因此该范围内第一个和最后一个值(0和1023)的概率仅为一半

entity tb is
end entity;

library ieee;
use ieee.math_real.uniform;
use ieee.math_real.floor;

architecture sim of tb is
begin
  process is
    variable seed1 : positive;
    variable seed2 : positive;
    variable x : real;
    variable y : integer;
  begin
    seed1 := 1;
    seed2 := 1;
    for n in 1 to 10 loop
      uniform(seed1, seed2, x);
      y := integer(floor(x * 1024.0));
      report "Random number in 0 .. 1023: " & integer'image(y);
    end loop;
    wait;
  end process;
end architecture;
在一篇评论中,我写了“寻找LFSR随机数生成器”,但使用这些生成器可能会有一些陷阱,这些陷阱可能不会立即显现出来

  • 它们不能生成值零
  • 它们不能产生两次相同的数字
  • 每个数字在2^n个周期中仅出现一次
  • 这两个数字之间有着高度的“相关性”
我编写并测试了一个16位生成器,试图解决上述一些问题。但请注意,结果总是一个随机数,因此不要期望真正的随机性

我使用Verilog是因为我写的速度大约是VHDL的五倍,但是对于VHDL程序员来说,翻译应该是微不足道的

//
// 16-bit Pseudo random number generator
// Using Linear Feedback Shift Register
//
// No copyright : Freeware, 18 Nov. 2018 
//
//

// The LFSR polynomial is taken from a (by now almost famous)
// application note from Xilinx.
// Those polynomials are selected for using minimal logic
// not for getting good 'randomness'.
// Using an LFSR alone is not good:
// - They can not generate the value zero.
// - They can not produce the same number twice.
// - Every number appears only once in 2^n cycles.
// - The numbers are highly 'related' by a factor two.
//  e.g. an 8-bit LFSR would take steps:
//  1, 2, 4, 8, 16, 32, 64, 128, 33, 66, 132, 41, 82 
//  
//  Therefore I use only 16 of 32 bits and re-order
//  the selected bits. Even then it may take a while before,
//  (what our feelings say!) a more random sequence of number
//  appears. 
//  
// 
// Other LFSRs polynomials (From Xilinx app. note) 
//  32 =  32,31,30,10,0 (used here)
//  40 =  40,21,19,2,0
//  48 =  48,28,27,1,0
//  56 =  56,22,21,1,0
//  64 =  64, 4, 3,1,0
//  72 =  72,53,47,6,0
//  80 =  80,38,35,3,0
//  88 =  88,72,71,1,0
//  96 =  96,49,47,2,0
// 112 = 112,45,43,2,0
// 128 = 128,29,27,2,0
// 
// 
// Know deficiencies:
// -  Using a seed of zero will only produce zeros. 
//    This module does not check that!
// -  Always running. Might add a 'next' number 
//    input for power savings.
// 

module prng16
// Do NOT change these parameters unless you know what you are doing
#(parameter poly    = 32'hC0000400, // max length 32, bit LFSR bits 31,30,10 are set
            degree  = 32
 )
( 
  input   reset_n,
  input   clk,
  input   set,
  input   [degree-1:0] seed,
  output  [15:0] rnd
);

reg [degree-1:0] lfsr,feedback;

   always @(posedge clk or negedge reset_n)
   begin
      if (!reset_n)
         // Futile attempt to start somewhat random.
         // I know: this value is too big for some values of 
         // degree, but better then too small.
         lfsr <= { {(degree/3){3'b101}},2'b10};
      else
         if (set)
            lfsr <= seed;
         else
            lfsr <= feedback;
   end

integer n;

   always @( * )
   begin
      feedback[0]= lfsr[degree-1];
      for (n=1; n<degree; n=n+1)         
        feedback[n] = poly[n]==1'b0 ? lfsr[n-1] : lfsr[n-1]^lfsr[degree-1];
   end

   // Pick 16 bits (indices randomly chosen)
   assign rnd = { lfsr[27], lfsr[16], lfsr[ 6], lfsr[22], 
                  lfsr[20], lfsr[ 0], lfsr[18], lfsr[26],
                  lfsr[10], lfsr[ 9], lfsr[25], lfsr[19], 
                  lfsr[11], lfsr[ 7], lfsr[28], lfsr[ 8]};

endmodule
//
//16位伪随机数发生器
//使用线性反馈移位寄存器
//
//无版权:免费软件,2018年11月18日
//
//
//LFSR多项式取自a(现在几乎是有名的)
//Xilinx的申请说明。
//选择这些多项式是为了使用最小逻辑
//不是为了获得好的“随机性”。
//单独使用LFSR并不好:
//-它们不能生成值零。
//-它们不能产生相同的数字两次。
//-每个数字在2^n个周期中仅出现一次。
//-数字与系数2高度“相关”。
//例如,8位LFSR将采取以下步骤:
//  1, 2, 4, 8, 16, 32, 64, 128, 33, 66, 132, 41, 82 
//  
//因此,我只使用32位中的16位并重新排序
//所选位。即使这样,也可能需要一段时间,
//(我们的感觉是什么!)一个更随机的数字序列
//出现了。
//  
// 
//其他LFSRs多项式(摘自Xilinx应用程序注释)
//32=32,31,30,10,0(此处使用)
//  40 =  40,21,19,2,0
//  48 =  48,28,27,1,0
//  56 =  56,22,21,1,0
//  64 =  64, 4, 3,1,0
//  72 =  72,53,47,6,0
//  80 =  80,38,35,3,0
//  88 =  88,72,71,1,0
//  96 =  96,49,47,2,0
// 112 = 112,45,43,2,0
// 128 = 128,29,27,2,0
// 
// 
//了解不足之处:
//-使用零种子只能产生零。
//此模块不检查该选项!
//-始终运行。可能会添加“下一个”号码
//输入功率节省。
// 
模块prng16
//除非您知道自己在做什么,否则不要更改这些参数
#(参数poly=32'hC0000400,//最大长度32,设置位LFSR位31,30,10
度=32
)
( 
输入复位,
输入时钟,
输入集,
输入[degree-1:0]种子,
输出[15:0]rnd
);
reg[degree-1:0]lfsr,反馈;
始终@(posedge clk或negedge重置)
开始
如果(!重置)
//徒劳的尝试开始有点随机。
//我知道:这个值对于某些值来说太大了
//学位,但最好不要太小。

lfsr。我是通过将你问题的标题插入谷歌找到的。在那篇文章的顶部还有一些更详细的链接。您好,谢谢您的回答,实际上我想创建一个随机数,在fpga bord上使用。我将创建一个冷热游戏并猜出随机数。我可以使用thıs codeın vhd fıle还是thısıs仅用于测试台?要获得更可合成的版本,请查看LFSR随机数生成器。这仅用于测试台。正如@Oldfart所说,请查看硬件实现。