Constraint value repetition

Hi,

Below is the code to get unique values on an array. I see the values getting repeated though. Can any one tell me what is the problem here?



module tb;

  class matrix;
    
    rand bit [4:0] arr [3][3];
         bit [4:0] list [$];
      
      
      constraint b {foreach (arr[i,j])
      {!(arr[i][j] inside {list});}
                   }
      
      function void post_randomize();
                  foreach (arr[i,j]) begin
                    list.push_back(arr[i][j]); end
                  endfunction
        
   
   
    
endclass : matrix
                   matrix m1;
               
                                                                     
                initial begin
                  
                  m1 = new();
                 
                  if (!m1.randomize())
                    $display("Error");
                  
                  $display ("%p",m1.arr);
                  $display ("%p",m1.list);
                
                    
                end
    
    endmodule

Output: '{'{8, 25, 22}, '{11, 20, 26}, '{8, 2, 5}}

In reply to rag123:

Hi, You are not randomizing for multiple times to make sure that, the list will be used once again. Please remember that post randomize will be executed only after the completion of randomization. That’s why you are not getting the expected result. Please find the below code to generate unique numbers.


module tb;

	class matrix;

		rand bit [4:0] arr [3][3];

		constraint c1{foreach(arr[i,j])
						foreach(arr[,k])
							if(j != k)
								arr[i][j] != arr[i][k];}
		
		constraint c2{foreach(arr[i,j])
						foreach(arr[k,])
							if(i != k)
								arr[i][j] != arr[k][j];}
	endclass : matrix

	matrix m1;

	initial 
		begin
			m1 = new();
			if (!m1.randomize())
				$display("Error");

			$display ("%p",m1.arr);
		end

endmodule

Hope this helps.
Putta Satish,
Maven Silicon Softech Pvt. Ltd.

In reply to rag123:

Hi,

I modified your code a bit and seems to be working as you expected


class matrix;
  rand bit [4:0] arr [3][3];
  constraint a_cst
  {
    unique {arr};
  }
    
endclass : matrix

module tb;
  matrix m1;
  initial 
    begin
      m1 = new();
      repeat(10)
        begin
          if (!m1.randomize())
            $display("Error");
          $display ("%p",m1.arr);
        end
    end

endmodule
 

In reply to puttasatish:

Satish, your values are repeating.

‘{’{'h1a, 'h17, 'hc}, '{'h7, 'h11, 'h0}, '{'hd, 'h1a, 'h8}}

In reply to nnd:

I am trying without using unique keyword.

In reply to rag123:

In reply to puttasatish:
Satish, your values are repeating.
‘{’{'h1a, 'h17, 'hc}, '{'h7, 'h11, 'h0}, '{'hd, 'h1a, 'h8}}

I have added one more constraint as below.


module tb;
 
	class matrix;
 
		rand bit [4:0] arr [3][3];
 
		constraint c1{foreach(arr[i,j])
						foreach(arr[,k])
							if(j != k)
								arr[i][j] != arr[i][k];}
 
		constraint c2{foreach(arr[i,j])
						foreach(arr[k,])
							if(i != k)
								arr[i][j] != arr[k][j];}
								
		constraint c4{foreach(arr[i,j])
						foreach(arr[k,l])
							if((i!=k)&&(j!=l))
								arr[i][j]!=arr[k][l];}						
	endclass : matrix
 
	matrix m1;
 
	initial 
		begin
			m1 = new();
			if (!m1.randomize())
				$display("Error");
 
			$display ("%p",m1.arr);
		end
 
endmodule

In reply to rag123:


class matrix;
  rand bit [4:0] arr [3][3];
  constraint block_unique {
        foreach   ( arr[i, j] ) {
          foreach ( arr[k, l] ) {
        if ( ! ( i == k && j == l ) ) {
          arr[i][j] != arr[k][l];
        }
      }
    }
  }
 
endclass : matrix
 
module tb;
  matrix m1;
  initial 
    begin
      m1 = new();
      repeat(10)
        begin
          if (!m1.randomize())
            $display("Error");
          $display ("%p",m1.arr);
        end
    end
 
endmodule

In reply to nnd:


  foreach   ( arr[i, j] ) {
          foreach ( arr[k, l] ) {
        if ( ! ( i == k && j == l ) ) {
          arr[i][j] != arr[k][l];

can you explain the above constraint?

In reply to rag123:

Any time both the foreach loops don’t point to the same entry in the grid, make the entries unique.