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.
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 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.”
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.