4

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
Ricardo
  • 6,134
  • 19
  • 52
  • 85
Abhi
  • 131
  • 1
  • 2
  • 11
  • http://stackoverflow.com/questions/13404737/generate-random-number-seed-changes-only-once – pjc50 Nov 14 '14 at 10:31

2 Answers2

6

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
pre_randomize
  • 1,180
  • 6
  • 11
4

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.

Greg
  • 4,270
  • 1
  • 21
  • 32
dave_59
  • 7,557
  • 1
  • 14
  • 26
  • "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? – Abhi Nov 17 '14 at 11:13
  • @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. – dave_59 Nov 18 '14 at 07:14
  • 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. – pre_randomize Nov 18 '14 at 09:00
  • @pre_randomize - correct. Each thread maintains its own seed. An each class object maintains its own seed as well. – dave_59 Nov 19 '14 at 05:23