4
\$\begingroup\$

In Verilog, $random generates different random inputs but this doesn't seem to be working when I try. Each time I use $random for a reg or integer, I must be getting different different values but that is not so. I tried it even in Modelsim simulator.

http://www.edaplayground.com/x/Umt

`timescale 1ns/1ns
module tt;
integer kk;

initial
begin
  repeat(5) begin
    kk=$random % 10;
  end
  $monitor("%d",kk);
end
endmodule
\$\endgroup\$
1

2 Answers 2

6
\$\begingroup\$

If you output all 5 random values you generate you can see that you get different values: EDA Playground.

The point of a seed is, given the same seed you can generate the same sequence. This allows you to have randomised tests but when one instance fails you can plugin the seed that was used and rerun the same test.

module tt;
  integer kk;
  integer seed = 1; //change this for different sequence
  initial
    begin
      repeat(5) begin
        kk=$random(seed) % 10;
        $display("%d",kk);
      end
    end

endmodule
\$\endgroup\$
4
\$\begingroup\$

Your problem is that you are generating 5 random values with 0 delay, and $monitor only prints a single line once per time slot when one of its arguments change. You should not be using $monitor except for rare debugging cases.

Also, I suggest that you use $urandom instead of $random. The seed for $urandom can be changed on the simulator command line without having to recompile. You can even select a random seed so that every time you simulate, it will use a new random seed (in ModelSim: -sv_seed random). $urandom has random stability that would be very difficult to achieve using $random. Random stability lets you make certain changes to your testbench and re-run the simulation with the same seed without disturbing the random numbers generated. This makes debugging much more predictable. See section 18.14 of the IEEE Std 1800-2012 LRM.

\$\endgroup\$
4
  • \$\begingroup\$ "Random stability lets you make certain changes to your testbench and re-run the simulation with the same seed without disturbing the random numbers generated". How is it possible? and what is Random stability? \$\endgroup\$
    – Abhi
    Nov 17, 2014 at 11:13
  • \$\begingroup\$ @pre_randomize - Not true $random uses a single internal seed-you will always get the exact same series of random numbers for each call to $random wherever that call may be. When you rerun a failing simulation to debug, you want the exact same series of random numbers to be generated in order to re-create the failure. That happens when you use either system function. The real problem is when you modify your code to correct the failure in either the DUT or TB, you want the same series of random numbers generated. That is what Random Stability strives to give you. Pls read the LRM section. \$\endgroup\$
    – dave_59
    Nov 18, 2014 at 7:14
  • \$\begingroup\$ From what I can tell 'Random Stability' implies that $urandom is thread safe. Order of execution of threads should effect the random sequences each receives. $random is not thread safe, each thread will effect the values the others see. \$\endgroup\$ Nov 18, 2014 at 9:00
  • \$\begingroup\$ @pre_randomize - correct. Each thread maintains its own seed. An each class object maintains its own seed as well. \$\endgroup\$
    – dave_59
    Nov 19, 2014 at 5:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.