Constraints

class packet;

	rand bit [5:0] array [];
	
	rand int i;
	rand int prev_i;
	
	constraint size_array {array.size() <10;
						  array.size != prev_i;}
	constraint elements {
							
							foreach (array[i])
							
									{
								
										array[i] inside {[0:64]}
									
									};
						
						}
						
	constraint unique_elements {unique {array[i]};} //to make sure no elements repeat at an instance of randomization
			   
	constraint inequality {array.size > 3;}  
	
	function void post_randomize();
	
		prev_i = array.size;
	
	endfunction
			   
endclass: packet


module foreach_constraint;

	initial 
	
		begin
		
			packet pkt = new;
			
			repeat (5)
			
				begin
			
					pkt.randomize();
					
					$display("\nThe size of the array is %0d",pkt.array.size());
			
					$display("elements of the array = %0p",pkt.array);
			
				end
		
		end


endmodule

The op:

The size of the array is 7
# elements of the array = 31 32 47 7 11 34 23
# 
# The size of the array is 9
# elements of the array = 55 33 59 32 53 25 23 43 41
# 
# The size of the array is 7
# elements of the array = 55 9 25 50 51 28 43
# 
# The size of the array is 8
# elements of the array = 29 37 12 24 6 49 24 42
# 
# The size of the array is 4
# elements of the array = 39 19 1 34

7 is randomized twice. I dont want the size to repeat again. What constraint should I use for that condition?

Thank you in advance

In reply to Shashank Gurijala:

randc int size;

constraint size_array {array.size() <10;
	array.size == size;
}

Declare an integer randc variable and put the constraint to match the array size.

In reply to yourcheers:

It’s not working :(


class packet;

rand bit [5:0] array [];
randc int arr_size;  

rand int i;
rand int prev_i;

  constraint size_array {array.size() <10;
 array.size==arr_size;}
constraint elements {

foreach (array[i])

{

array[i] inside {[0:64]}

};

}

constraint unique_elements {unique {array[i]};} //to make sure no elements repeat at an instance of randomization

constraint inequality {array.size > 3;}

function void post_randomize();

//prev_i = array.size;

endfunction

endclass: packet

module foreach_constraint;

initial

begin

packet pkt = new;

repeat (5)

begin

pkt.randomize();

$display("\nThe size of the array is %0d",pkt.array.size());

$display("elements of the array = %0p",pkt.array);

end

end

endmodule


In reply to rag123:

The o/p i’m receiving is this:

In reply to Shashank Gurijala:

Please use code tags making your code easier to read. I have added them for you.

Do not declare loop iterator variables (i) separately outside their loops.

Your original example only makes sure that adjacent size values do not repeat. randc is what you want.

class packet;
  rand bit [5:0] array [];
  randc int size;
 
  constraint size_array {size inside {[4:9]}; array.size == size;}
  constraint elements { foreach (array[i])
    array[i] inside {[0:64]};
                      }
  constraint unique_elements {unique {array};} 
endclass: packet

module foreach_constraint;
  packet pkt = new;
  initial
    begin
      repeat (5)
        begin
          assert(pkt.randomize());
          $display("\nThe size of the array is %0d",pkt.array.size());
          $display("elements of the array = %0p",pkt.array);
        end
    end
endmodule