Rand behaviour using randc

HI All,

This is the code to get rand behaviour using randc keyword.

module p1;
  class c1;
    randc bit [2:0] addr;
    function void post_randomize();
      $display("unique value %0d",addr);
    endfunction
  endclass
  c1 obj;
  initial begin
    repeat (8) begin
    obj = new;
    obj.randomize();
    end
  end
endmodule

The output of the code contains some repeated values since I am constructing obj eight times.

I am curious to find/know, what is the alternative way to get the same rand output by using randc.
Please suggest another alternative method without constructing objects eight times, I would like to get rand behavior from randc.

Thank You,

In reply to Thirumalesh Kumar:


 class c1;
    randc bit [2:0] addr;
    rand  bit [2:0] addr1;    
    constraint EQUAL { addr1 == addr ; }
 endclass
 c1 obj;
  initial begin
    obj = new;
    repeat (8) begin
    if( !obj.randomize() ) $display("Randomization fails !!");
    end
  end

In reply to ABD_91:

Please find the working code below

module top;
  class c1;
    randc bit [2:0] addr;
    function void post_randomize();
      $display("unique value %0d",addr);
    endfunction
  endclass
  c1 obj;
  initial begin
    obj = new;
    repeat (9) begin
    void'(obj.randomize());
    end
  end
endmodule

I’m constructing the object once. Afterwards I’m iterating the randomization.

In reply to chr_sue:

I’m constructing the object once. Afterwards I’m iterating the randomization.

This doesn’t meet the requirement of “rand behaviour using randc”
Both of our solutions don’t meet the requirement :(

HI ABD_91 and chr_sue, Thank you for the reply.

Above code will not give the solutin and I am sorry if I would have asked in the wrong way.

Here is the requirement: using randc, I want to get normal random values which may repeat.
For example: 0 1 2 2 7 6 5 5

I do not want see all possible values like : 0 1 2 3 4 5 6 7

How to get random numbers with some are repeated using randc?

Thank You,

In reply to Thirumalesh Kumar:
I believe this is impossible. The SV LRM says
“Variables declared with the randc keyword are random-cyclic variables that cycle through all the values in a random permutation of their declared range. The basic idea is that randc randomly iterates over all the values in the range and that no value is repeated
within an iteration.”

In reply to Thirumalesh Kumar:

The reason you appear to be getting rand behavior from a randc member is because you are constructing a new object each time. Each time you construct an object, it gets seeded by the next random state value (RNG) from the process constructing the object. That starts a new random cyclic series of numbers which has a 1 in 8 chance of repeating the start of the previous series.

To get the behavior you are looking for without constructing a new object, you would have to manually re-seed the object using obj.srandom() before calling obj.randomize() each time. That would break the random stability of obj, but that is the best answer you are going to get.

In reply to dave_59:

Dave,
I did try the srandom() approach:


 initial begin
    obj = new();
    repeat (9) begin    
      void'(obj.randomize());
      obj.srandom($urandom()); // Is this correct  ?
    end
  end

I still observe randc behavior