Does randc loses cyclic random nature for multidimensioal randc arrays?

Hi,

I wanted to generate 8 4 bit random numbers cyclic in nature.

I wrote following code.

module rand1;



class expt;
randc bit [3:0] a[0:7];
endclass 




initial begin 

expt e1 = new();


foreach(e1.a[i]) begin	
e1.randomize();
	$display("a[%d]=%d\n",i,e1.a[i]);
end


end




endmodule

What I am getting is this.6 is repeated. Looks like cyclic random numbers are not getting generated. Please help.

a[ 0]= 6

a[ 1]= 3

a[ 2]=11

a[ 3]= 0

a[ 4]=15

a[ 5]=12

a[ 6]= 6

a[ 7]=11

In reply to suyog_asic:

Randc can’t be used in that way for array to get unique elements.
LRM said:


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

You should use rand with unique constraint (move the randomize statement outside of the loop):


module rand1;
class expt;
rand bit [3:0] a[0:7];
  constraint c_unique{
    unique{a};
  }
endclass

initial begin
  expt e1 = new();
  e1.randomize();
  foreach(e1.a[i]) begin	
    $display("a[%0d] = %0d",i ,e1.a[i]);
  end
end
endmodule

Noted: unique constraint is very expensive, it is difficult to be solved. SV will take very long time to solve unique constraint for big array size.

In reply to chris_le:

Thats a great input Chris. Thanks a lot.

Yes thats right, every element is randc itself but not with each other.

I modified my code and saw the behavior you mentioned.

module rand1;

class expt;
randc bit [3:0] a[0:7];
endclass

initial begin

expt e1 = new();
foreach(e1.a[m] ) begin
for(int i = 0;i<16;i++) begin
e1.randomize();
$display(“a[%d]=%d”,m,e1.a[m]);
end
end

end

endmodule

I am getting output like this. Every element of array is random cyclic.

a[ 0]= 6
a[ 0]=14
a[ 0]= 7
a[ 0]= 8
a[ 0]= 1
a[ 0]= 0
a[ 0]= 3
a[ 0]=11
a[ 0]= 2
a[ 0]=15
a[ 0]=12
a[ 0]= 4
a[ 0]= 9
a[ 0]= 5
a[ 0]=10
a[ 0]=13
a[ 1]=13
a[ 1]= 3
a[ 1]=14
a[ 1]=10
a[ 1]=11
a[ 1]= 8
a[ 1]= 0
a[ 1]=12
a[ 1]= 4
a[ 1]= 5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