Generate unique elements in an array

In reply to edaboy:

Are you sure you are not getting any other warnings or error messages before the displayed output. The tool you are using has lots of problems. Consider a different tool or upgrading to a more recent version.

The first constraint should be an error because it would try to create the constraint num[10] !=num[9], and num[10[ is out of bounds. You need to write it as

  constraint rand_num{foreach (num[i]) i+1 < $size(num) -> num[I+1] != num[i];};

The other two constraints work for me.

In reply to dave_59:

Thanks Dave for quick reply. I did not see any error or warning. I still see weird behavior with code that you asked me to modify. However with that code i see that both the other constraint works fine.
notice that i modified code , converted bit to int and i want to make sure int are generated in specific range. is it a proper way to do it? or any other better way can you suggest ?>

constraint rand_num{foreach (num[i]) i+1 < $size(num) → num[I+1] != num[i];};



  rand int unsigned  num [10];
  rand int unsigned  data[10];
  rand int unsigned  uniqnum[10];

  constraint rand_num{foreach (num[i]) i+1 < $size(num) -> num[i+1] != num[i];
                      foreach (num[i]) num[i] inside {[0:20]};   
                  };
  constraint data_values { foreach(data[i]) 
                               foreach(data[j])
                                 if(i != j) data[i] != data [j] ;} 
  constraint rand_unitnum{unique {uniqnum};}
[\systemverilog]


num[0] 11
num[1] 10
num[2] 5
num[3] 18
num[4] 1
num[5] 8
num[6] 17
num[7] 6
num[8] 17
num[9] 12
uniqnum[0] 2585900447
uniqnum[1] 2885502533
uniqnum[2] 4138482668
uniqnum[3] 3688754642
uniqnum[4] 565161356
uniqnum[5] 3727708039
uniqnum[6] 1611526103
uniqnum[7] 1088663520
uniqnum[8] 405083532
uniqnum[9] 1919651944
data[0]  3040365644
data[1]  1549177249
data[2]  2520948884
data[3]  671157517
data[4]  3642241582
data[5]  1668691443
data[6]  2694328522
data[7]  616240321
data[8]  2242663180
data[9]  3757757259

In reply to dave_59:

Hi Dave ,
How to generate unique values for array of array without using unique ?

 constraint c {
	    foreach(fx_arry[i]) {
       	     // fx_arry[i] inside{[0:SIZE-1]};
               foreach(fx_arry[j]) {
       		  if(j != i){
		   fx_arry[j] !=fx_arry[i]
		  };
               }
            }
         }

I am unable get the logic right .can you help out.

In reply to rambarki.satish:

It seems to be a typo Satish.

constraint fx_arry_size_c{
    fx_arry.size() inside {[1:100]};
}

constraint uniq_c {
    foreach(fx_arry[i]) {   	
        foreach(fx_arry[j]) {
       		if(j != i){
		        fx_arry[j] !=fx_arry[i];
		    }
       }
    }
 }

In reply to deepthig:

I didn’t get the expected output after using the above approach.What is wrong in my code

class a;
  
  rand bit [3:0]a[15];
  
        
        constraint uniq_c {
          foreach(a[i]) {   	
            foreach(a[j]) {
       		if(j != i){
              a[j] !=a[i];
		    }
       }
    }
 }
        
        
   
  
function void post_randomize();
a.shuffle();
    endfunction
endclass

 
module test;
  a a_i;
  int temp,i=0;
  initial begin
  a_i = new();
 
    foreach(a_i.a[i])begin
        a_i.randomize();
        temp=a_i.a[i];
      $display("array value[%d] =%d",i,temp);
      
    end  
     
      
#100;
  end
endmodule

output:

array value[ 0] = 3
array value[ 1] = 5
array value[ 2] = 3
array value[ 3] = 6
array value[ 4] = 5
array value[ 5] = 4
array value[ 6] = 10
array value[ 7] = 8
array value[ 8] = 4
array value[ 9] = 14
array value[ 10] = 1
array value[ 11] = 7
array value[ 12] = 11
array value[ 13] = 15
array value[ 14] = 13

In reply to Bharat100:
Move
a_i.randomize();
in front of the foreach loop. Also, there is no need to shuffle() the array.

In reply to dave_59:

That was my fault You are right. Thank you Dave.