Randomization and Disabling Randomization

 random variables 

Class variables can be declared random using the rand and randc type-modifier keywords.

following types can be declared as rand and randc,
  • singular variables of any integral type
  • arrays
  • arrays size
  • object handle's


Variables declared with the rand keyword are standard random variables. Their values are uniformly distributed over their range.

    rand bit [3:0] addr;
addr is an 4-bit unsigned integer with a range of 0 to 15. on randomization this variable shall be assigned any value in the range 0 to 15 with equal probability.

 randc: random-cyclic 

Variables declared with the randc keyword, their values doesn't repeat a random value until every possible value has been assigned.
   randc bit   wr_rd;

In order to randomize the object variables, need to call randomize() method.


In the example below,
Two variables addr1 and addr2 of same bit type is declared as rand and randc respectively, observe the randomized values of addr1 and addr2.

class packet;
  rand  bit [2:0] addr1;
  randc bit [2:0] addr2; 

module rand_methods;
  initial begin
    packet pkt;
    pkt = new();
    repeat(10) begin
      $display("\taddr1 = %0d \t addr2 = %0d",pkt.addr1,pkt.addr2);

 Simulator Output  

addr1 = 6 addr2 = 4
addr1 = 4 addr2 = 3
addr1 = 2 addr2 = 0
addr1 = 6 addr2 = 6
addr1 = 1 addr2 = 7
addr1 = 3 addr2 = 5
addr1 = 7 addr2 = 1
addr1 = 6 addr2 = 2
addr1 = 4 addr2 = 7
addr1 = 4 addr2 = 6
Execute the above code on 

 Disabling random variables 

The rand_mode() method can be used to disable the randomization of variable declared with rand/randc.

The syntax for the rand_mode() method is:
                addr.rand_mode(0); //disable randimization of addr

1.By default rand_mode value for all the random variables will be 1.
2.after setting rand_mode(0) to any random variable, it will get randomized only after rand_mode(1).