如何重新设定静态进程的 RNG? [英] How to reseed the RNG of a static process?

查看:23
本文介绍了如何重新设定静态进程的 RNG?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 always 进程在我的测试平台中运行,它调用 $urandom_range() 是否可以在我运行我的测试平台时重新设定这个进程?

I have an always process running in my testbench that calls $urandom_range() Is it possible to reseed this while im running my testbench?

我猜它与 srandom 有关,但无法让它工作.

I guess it has something to do with srandom but can't get it to work.

推荐答案

可以通过调用 为线程(即 $urandom 等使用的)生成随机数生成器的种子$urandom 带有整数参数,例如:

It is possible to seed the random number generator for the thread (ie that used by $urandom etc) by calling $urandom with an integer argument, eg:

 $urandom(12345);

你提到了 srandom.这是与线程的随机数生成器交互的另一种方式,即使用 process 类,该类是在 std 包中声明的类.这个进程类有一个名为 srandom 的方法,它从一个整数中作为当前线程的种子.

You mention srandom. This is another way to interact with the thread’s random number generator and that is by using the process class, which is a class declared in the std package. This process class has a method called srandom which seeds the current thread from an integer.

要使用process类,首先你需要一个process类的变量:

To use the process class, first you need a variable of class process:

   std::process p;

然后你需要用进程类的一个静态方法的返回值来分配这个变量,叫做self:

and then you need to assign this variable with the return value of a static method of the process class, called self:

   p = process::self(); 

如果您不熟悉面向对象设计,请不要担心.相信我.这是你需要做的.把这两行想象成某种魔法咒语.一旦我们发出了这个魔法咒语,我们就可以调用 srandom 方法来播种线程的随机数生成器,例如:

If you're not familiar with object-oriented design, don't worry. Trust me. This is what you need to do. Think of these two lines as like some kind of magic spell. Once we have issued this magic spell, we can call the srandom method to seed the thread’s random number generator, eg:

  p.srandom(12345);

那么,srandom$urandom 有什么区别? 或者换一种说法:

So, what’s the difference between srandom and $urandom? Or to put it another way:

如果我使用进程类的 srandom 方法设置种子,是否将线程的随机数生成器设置为与使用 $urandom?

If I set a seed using the srandom method of the process class, does that set the thread’s random number generator to the same state as setting the same seed using $urandom?

没有区别.除非有.

什么?让我们把一些代码(在这个答案的底部放到)EDA Playground:https://www.edaplayground.com/x/4yN4

What? Let’s put some code (as at the bottom of this answer into) EDA Playground: https://www.edaplayground.com/x/4yN4

让我们使用 srandom 为线程的随机数生成器做种子:

Let’s seed our thread’s random number generator using srandom:

   p.srandom(9);

然后让我们生成四个随机数:

then let’s generate four random numbers:

   repeat(4) $display( $urandom ); 

看看我们得到了什么:

# KERNEL:  659167701
# KERNEL: 3562992094
# KERNEL: 2163162795
# KERNEL: 4088777565 

然后让我们使用 $urandom:

   $urandom(9);

再次生成四个随机数

  repeat(4) $display( $urandom ); 

再次让我们看看我们得到了什么:

and again let’s see what we get:

# KERNEL: 3562992094
# KERNEL: 2163162795
# KERNEL: 4088777565
# KERNEL: 3967046865 

乍一看,我们好像生成了不同的四个随机数.但是,仔细检查后,我们发现第二个 ($urandom) 系列的第一个数字与第一个系列的第二个数字 (srandom) 相同.为什么会这样?嗯,这是因为调用 $urandom 设置种子不仅设置了种子,还生成了一个随机数,而调用 srandom 只设置了种子.

At first glance, it looks like we've generated a different four random numbers. However, on closer inspection, we see that the first number of the second ($urandom) series is the same as the second number of the first series (srandom). Why might that be? Well, it's because the call to $urandom to set the seed not only set the seed, but also generated a random number, whereas the call to srandomonly set the seed.

module M;
  initial
    begin
      string randstate;
      std::process p;
      p = process::self();
      $display("p.srandom(9)");
      p.srandom(9);
      repeat(4) $display( $urandom );
      $display("$urandom(9)");
      $urandom(9);
      repeat(4) $display( $urandom );
    end
endmodule

<小时>

另见我在此处的回答,其中包含有用的背景资料:


See also my answer here, which has useful background stuff:

urandom_range()、urandom()、verilog 中的 random()

这篇关于如何重新设定静态进程的 RNG?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆