Randomizing 2d array

Hello! Look at the code below:

class packet;
  
    rand bit [7:0] array[][];
  
    rand int i,j;
  
    rand int r,c;
  
    randc int size_i;
 
    randc int size_j;
  
    constraint unique_elements {
	
		foreach (array[i,j]) {
  	                    
                             foreach (array[r,c]) {											
																			  
                                                 (!(r == i && c == j)) -> !(array[r][c] == array [i][j]);								
																			 
                                                   }
															
	         		      }
													  
			        }
							 
    constraint rand_size {
	
		foreach (array [i,j]) {
																																              										 
                          array.size [i] == size_i;
																																
	                  array.size [j] == size_j;
														
				       }
												  
			   }
						
    constraint size_const {size_i < 10; size_j < 10; size_i != size_j; size_i != 0; size_j != 0;}
 
  
  function void display();
  
	for (int i = 0; i < size_i; i++) 
	
		begin
		
			$display("\n\n");
      
			for (int j = 0; j < size_j; j++) 
			
				begin
					
					$display("\narray [%0d][%0d]  \t=\t  %0d",i,j,array[i][j]); 
				
				end 
    
		end
  
  endfunction 

endclass
 
 
 
module unique_elements;
 
  initial

	begin
    
		packet pkt=new();
      
		pkt.randomize;
      
		pkt.display();
	
	end

endmodule

I’m trying to randomize and display unique elements in a 2d array w/o using unique keyword. There are no syntax errors and I receive no output.

Please help. Thanks!

In reply to Shashank Gurijala:

I’d recommend you to try to print the variables you are using and understand the values and constraints to see what the issue could be for example try understand the data type of size_i,j and the result you get, also why are you declaring i,j,r,c, how are they intended to be used according to your logic?
Your code for the rand_size constraint doesn’t make sense to me (I could be wrong)

Since array is a dynamic array of arrays,IMHO it should be something like this:

array.size() == size_i; // sets the number of arrays
foreach(array[i]){
array[i].size() == size_j;// set the size of each array
}

Always check the return value of the randomize call i.e if(!pky.randomize())$fatal(…)
Finally there are several entries in this forum about this question/exercise.

HTH,
-R

In reply to rgarcia07:

Hello rgarcia07!

This is working perfectly fine. Thanks! Here is the edited code.

class packet;
  
  rand bit [500:0] array[][];
  
  ///rand int i,j;
  
  //rand int r,c;
  
  randc int size_i;
 
  randc int size_j;
  
    constraint unique_elements {
	
                    foreach (array[i,j]) {
 												  
                                 foreach (array[r,c]) {
																																				  
                              (!(r == i && c == j)) -> !(array[r][c] == array [i][j]);
														
																			 
                                                         }
															
													  
                                             }
													  
				    }
		
//randomizing both rows and  columns of a 2d array
		
    constraint rand_size {
	
			array.size == size_i;
							
			foreach (array [size_i]) {
																																									 
                                      array [size_i].size == size_j;
														
												     
                                                 }
												  
			  }
						
    constraint size_const {size_i < 6; size_j < 11; size_i > 0; size_j > 0;}
	
    constraint array_element {
	
			foreach (array [i,j]) {
								
														 
                                   array [i][j] < 500;
														
														 
                                   array [i][j] > 100;
														
													   
                                               }
													   
			       }
 
  
  function void display();
  
	for (int i = 0; i < size_i ; i++) 
	
		begin
		
			$display("\n");
      
			for (int j = 0; j <size_j; j++) 
			
				begin
					
		$display("\narray [%0d][%0d]  \t=\t  %0d",i,j,array[i][j]); 
				
				end 
    
		end
  
  endfunction 

endclass
 
 
 
module unique_elements;
 
  initial

	begin
    
		packet pkt=new();
      
		repeat (5) 
		
			begin
	  
				pkt.randomize;

$display ("\nThe dimensions of the array: Rows [i] = %0d and Columns [j] = %0d",pkt.size_i,pkt.size_j);
      
				pkt.display();
				
	$display("\n-------------------------------------------------------");
				
			end
			
	end

endmodule