Assigning the elements in a 3d array

Hello! Look at the below code. Im trying to assign the elements as follows:

			
class packet;

	rand int  array [] [] []; //diff b/w randc bit [3:0] [1:0] array [] [] [] and randc bit [7:0] array [] [] []
	
	randc int size_i, size_j, size_k;
	
	static constraint c1 {
	
				array.size == size_i;

				foreach (array [size_i]) {
						
					array [size_i].size == size_j;
													
					foreach (array [size_i,size_j]) {
													
																						 
                                              array [size_i][size_j].size == size_k;
																						
																					 
                                                                            }
																					
												 }
												 
				   } //c1 is the constraint to randomize rows, columns and height of a 3d array
	
	static constraint c2 {size_i > 0; size_j > 0; size_k > 0;}
	
	constraint c3 {size_i <= 5; size_j <= 5; size_k <= 5;}
	
	constraint c4 {size_i != size_j && size_j != size_k && size_i != size_k;}
	
	constraint c5 {unique {array};}
	
	static constraint c6 {
				foreach (array [size_i]) {
						
					foreach (array [size_j]) {
													
						foreach (array [size_k]) {
																				
						       if (size_i % 2 == 0) {
																											
								if (size_j % 2 == 0) {
																																
								   if (size_k % 2 == 0) {
																																					
							               array [size_i] [size_j] [size_k] == 222;
																																										
									                 }
																																									
								    else if (size_k % 2 != 0) {
																																					
									array [size_i] [size_j] [size_k] == 221;
																																										
								                     }
																																				
								}
																																						
							if (size_j % 2 != 0) {
																																
									if (size_k % 2 == 0) {
																																					
										array [size_i] [size_j] [size_k] == 212;
																																									
							                                     }
																																									
					                                if (size_k % 2 != 0) {
																																					
										array [size_i] [size_j] [size_k] == 211;
																																											
									                   }
																																										 
								}
																				
									}
																															
			if (size_i % 2 != 0) {
																											
					if (size_j % 2 == 0) {
																																	
								if (size_k % 2 == 0) {
																																						
										array [size_i] [size_j] [size_k] == 122;
																																											
								}
																																										
						if (size_k % 2 != 0) {
																																						
					array [size_i] [size_j] [size_k] == 121;
																																												
									}
																																										
						}
																																				
		if (size_j % 2 != 0) {
																																	
					if (size_k % 2 == 0) {
																																						
						array [size_i] [size_j] [size_k] == 112;

						}
																																										
				if (size_k % 2 != 0) {
																																						
			array [size_i] [size_j] [size_k] == 111;
    																																												
						}
																																											 
						}
																																					
		}
																																 
																										}

																			}
																			
												}
												
					}
				 
endclass
				
module md_array_foreach_constraint;

	initial 
	
		begin
		
			packet pkt = new;
			
			$display("\n----------------------------------------DISPLAYING ARRAY ELEMENTS-----------------------------------------------\n");
			
			repeat (2)
			
				begin
			
					pkt.randomize();
					
$display ("\nThe dimensions of the randomized 3d array: Rows [i] = %0d , Columns [j] = %0d , Height [k] = %0d",pkt.size_i,pkt.size_j,pkt.size_k);
					
					for (int i = 0; i < pkt.size_i; i++)
					
						begin
							
							for (int j = 0; j < pkt.size_j; j++)
							
								begin
								
					$display ("\t"); //how is this tab space working in the program?
									
									for (int k = 0; k < pkt.size_k; k++)
									
										begin
										
					$display ("array [%0d]\t[%0d]\t[%0d] \t=\t  %0d",i,j,k,pkt.array[i][j][k]);
											
										end
										
								end
								
						end
					
	$display ("\n-----------------------------------------------------------------------------------------------------------");
					
				end
				
		end
		
endmodule

Can you please tell me whats wrong with the code

you have a contradicting constraint in C5 and C6. you are asking the array to be unique at the same time from constraint C6 you are trying to assign the solver fails. It would be helpful to show what you want to achieve with expected input and expected output.




   (array[0][0][1] == 221);
   (array[2][0][1] == 221);


In reply to rag123:

Say for array [1] [0] [1] where i is odd, j is even and k is odd, I want 121 to be printed in that index and hence the pattern follows.

I understood the unique constraint and removed it. Then too i didn’t get the output as I wanted.

In reply to Shashank Gurijala:

Got the right answer after replacing the nested foreach loops with a single foreach(size_i,size_j,size_k) {

And also comment the constraint c5 (or remove/disable)

Posting thee code here:

class packet;

	rand bit [25:0] array [] [] [];
	
	rand int size_i, size_j, size_k;
	
	static constraint c1 {
	
						array.size == size_i;

						foreach (array [size_i]) {
						
													array [size_i].size == size_j;
													
													foreach (array [size_i,size_j]) {
													
																						array [size_i][size_j].size == size_k;
																						
																					}
																					
												 }
												 
				   } //c1 is the constraint to randomize rows, columns and height of a 3d array
	
	constraint c2 {size_i > 0; size_j > 0; size_k > 0;}
	
	constraint c3 {size_i <= 5; size_j <= 5; size_k <= 5;}
	
	constraint c4 {size_i != size_j && size_j != size_k && size_i != size_k;}
	
	constraint c5 {
	
					foreach (array [size_i,size_j,size_k]) {
						
															if (size_i % 2 == 0) {
																											
																				  if (size_j % 2 == 0) {
																																
																										if (size_k % 2 == 0) {
																																					
														   																	    array [size_i] [size_j] [size_k] == 222;
																																										
																															 }
																																									
																										if (size_k % 2 != 0) {
																																					
																																array [size_i] [size_j] [size_k] == 221;
																																										
																															 }
																																				
																										}
																																						
																				  if (size_j % 2 != 0) {
																																
																										if (size_k % 2 == 0) {
																																					
																																array [size_i] [size_j] [size_k] == 212;
																																									
									 																		 	             }
																																									
																										if (size_k % 2 != 0) {
																																					
																																array [size_i] [size_j] [size_k] == 211;
																																											
																															 }
																																										 
																										}
																				
																				  }
																															
															if (size_i % 2 != 0) {
																											
												  								  if (size_j % 2 == 0) {
																																	
																										if (size_k % 2 == 0) {
																																						
																																array [size_i] [size_j] [size_k] == 122;
																																											
																														     }
																																										
																										if (size_k % 2 != 0) {
																																						
																																array [size_i] [size_j] [size_k] == 121;
																																												
						   																								      }
																																										
																										}
																																				
																				   if (size_j % 2 != 0) {
																																	
																										if (size_k % 2 == 0) {
																																						
																																array [size_i] [size_j] [size_k] == 112;

																															 }
																																										
																										if (size_k % 2 != 0) {
																																						
																																array [size_i] [size_j] [size_k] == 111;
																																												
																															 }
																																											 
																									    }
																																					
																				 }
																																 
															}

				  }
																					 
endclass
		

		
module md_array_foreach_constraint;

	initial 
	
		begin
		
			packet pkt = new;
			
			$display("\n----------------------------------------DISPLAYING ARRAY ELEMENTS-----------------------------------------------\n");
			
			repeat (5)
			
				begin
			
					pkt.randomize();
					
					$display ("\nThe dimensions of the randomized 3d array: Rows [i] = %0d , Columns [j] = %0d , Height [k] = %0d",pkt.size_i,pkt.size_j,pkt.size_k);
					
					for (int i = 0; i < pkt.size_i; i++)
					
						begin
							
							for (int j = 0; j < pkt.size_j; j++)
							
								begin
								
									$display ("\t"); //how is this tab space working in the program?
									
									for (int k = 0; k < pkt.size_k; k++)
									
										begin
										
											$display ("array [%0d]\t[%0d]\t[%0d] \t=\t  %0d",i,j,k,pkt.array[i][j][k]);
											
										end
										
								end
								
						end
					
					$display ("\n-----------------------------------------------------------------------------------------------------------");
					
				end
				
		end
		
endmodule

What I want to now know is the difference b/w foreach(size_i){foreach(size_j){foreach(size_k)
…}}} and foreach(size_i,size_j,size_k){…} in general and in this specific example as well.

Thank you!