使用UNIFORM的随机生成器

时间:2015-06-09 10:44:57

标签: vhdl uniform

我正在尝试为一个小游戏生成一个随机数,我尝试使用MATH_REAL包中的UNIFORM函数。

我在测试代码时得到一个值,但它永远不会改变。我试图让代码在每个rising_edge上发生,但它并没有改变任何东西。我无法找到或想到有效的解决办法。

我想我知道问题只是我的随机数只生成一次,但我无法找到改变它的方法。

这是我实施的(我的时钟变化):

    rand: process (count1) 
        variable seed1, seed2 : positive;
        variable rand : real;
        variable int_rand : integer range 1 to 5;
    begin
        if rst_i='1' then
            count1 <= 1;
        elsif rising_edge(clk_i) then
            UNIFORM(seed1, seed2, rand);
            int_rand := INTEGER(TRUNC(rand*5.0));
            count1 <= int_rand;
        end if;
    end process;

然后在另一个进程中使用count1的值。上面的代码简单地给出了相同的模拟结果:

    rand: process (count1) 
        variable seed1, seed2 : positive;
        variable rand : real;
        variable int_rand : integer range 1 to 5;
    begin
        UNIFORM(seed1, seed2, rand);
        int_rand := INTEGER(TRUNC(rand*5.0));
        count1 <= int_rand;
    end process;

是否有一种简单的方法可以确保多次生成随机数?

1 个答案:

答案 0 :(得分:1)

当目的是为count1的每个上升沿在clk_i上生成随机数时,请将进程的敏感性列表更新为(假设rst_i异步重置):

rand: process (rst_i, clk_i)

这会使流程在rst_iclk_i的每次更改时重新执行,其中count1的敏感度列表仅在count1更改时重新执行。

请注意,variable int_rand : integer range 1 to 5;应为range 0 to 4,因为统一结果为&gt; 0.0和&lt; 1.0。

一个有趣的行为是,rand: process (count1)的敏感性列表与第二个进程一样,if上没有count1保护代,实际上会使进程重新执行多次模拟,直到生成的count1值与先前的count1值相同,在这种情况下,count1上没有更改以触发流程的重新执行,然后不执行流程更多。