Randc problem

why problems in using randc?why sometimes it not randomize cyclic…elaborate about this keyword…and why we we not prefer to use it…

randc has many restrictions that prevent it from producing what appears to be non-cyclic. Anytime you modify a constraint on a randc variable, the cycle restarts. For example suppose you had this

claaa A
  randc bit [7:0] value; // random cycle variable
  bit [8:0] limit;  / state variable
  constraint lt {value < limit;}
endclass

A a;
initial begin
       a = new();
       a.limit = 3;
       repeat (3) begin 
             a.randomize();
             $display(a.value);
            end
       a.limit = 4;
       a.randomize();
       $display(a.value);
  end

The repeat loop will display 0,1,2 in some random order. But the 4th $display statement does not necessarily print 3. It will be the start of a new cycle of 0,1,2,3 in some random order. But it is worse than this. If you do anything that has the potential to change the solution space (the set of values that satisfy the constraints), the cycle may be broken. The tool is not required to compare the old space with the new space.

A better solution is to build a list of previously used values and use an inside operator constraint. You can reset the cycle by clearing the list after some number of iterations, or when the constraint fails.

module top;
  
class random;
  randc bit [3:0] b;
  bit [3:0] ua [16];
  randc bit [3:0] c [16];

endclass
  
  initial begin
    random r;
    r = new();
    assert(r.randomize(c))
    foreach(r.ua[i]) begin
      assert(r.randomize(b));
      r.ua[i] = r.b;
    end

    $display("Rand cyclic array c= %p\n",r.c);
    $display("Rand cyclic array ua= %p\n",r.ua);
  end
  

When I randomize the array “c” I see duplicate values but when I randomize “b” and assign it to array “ua” I see unique values as expected. Why wouldnt I get unique values in array “c”? Can you please explain?

In reply to bala2305:

Because the rand qualifier applies to unique solutions for all permutations of element values, not unique values of each element. You need to use a unique constraint to get all elements unique.

In reply to dave_59:

Thanks. So if i understand this right, doing the following, am going to get unique values on c[0] on each iteration and unique values on c[1] for each iteration. Right?

module top;
  
class random;
  randc bit [3:0] b;
  bit [3:0] ua [3];
  randc bit [3:0] c [2];

endclass
  
  initial begin
    random r;
    r = new();
    repeat(16) begin
      assert(r.randomize());
      $display("Rand cyclic array c= %p\n",r.c);
    end


  end
  
endmodule   

In reply to bala2305:

Yes. The LRM says:

Arrays can be declared rand or randc, in which case all of their member elements are treated as rand or randc.