问题描述
我需要在vhdl中生成一个介于0到1023之间的随机整数,但是我在互联网上找不到很好的资源.请问有人帮我吗?
I need to generate a random integer between 0 - 1023 in vhdl however I couldn't find a good resource for this on the internet. Is there anyone help me please ?
推荐答案
以下是生成范围为[0:1023]且具有均匀(偶数)分布的整数的示例.
Below is an example for generation of integers in range [0 : 1023] with uniform (even) distribution.
请注意,为了确保[0:1023]中所有整数值的均匀(均匀)分布,必须在与最大值+ 1相乘后使用floor
运算,在这种情况下为1023 + 1 = 1024. ,因为使用注释中的无底整数(因此为integer(x * 1023)
)(例如在示例在VHDL测试台中生成随机数")将导致四舍五入到最接近的值(并且从一半向上取整),因此,第一个和最后一个仅给出一半的概率值(0和1023).
Note that the floor
operation must be used after multiplication with the max value + 1, in this case 1023 + 1 = 1024, in order to ensure uniform (even) distribution of all integer values in [0 : 1023], since using integer without floor (thus integer(x * 1023)
) as in the example "Generating random numbers in a VHDL testbench" from the comment will cause rounding to nearest (and up from half), thus give only half probability for the first and last values (0 and 1023) in the range.
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;
这篇关于在VHDL中生成随机整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!