Constraint For Specific Value!

class base_b1;

   rand bit [31:0] reg_address = 0;
   rand bit [31:0] reg_address_array[$];

   constraint b1_c {
      reg_address dist {5:/25, 10:/25, [25:30]:/50};
   }

   constraint size_c2 { 
      reg_address_array.size inside {[9:10]};
   }
endclass: base_b1 

//----------------------------------
//------------ module top ----------
//----------------------------------
module tb_top;

   base_b1 h_b1;

   initial begin
      $display("This is module top !!");

      h_b1 = new();

      assert(h_b1.randomize());
      for (int i=0; i<h_b1.reg_address_array.size(); i++) begin
         h_b1.reg_address_array[i] = h_b1.reg_address;
         $display("This is base class !! and reg_address is %0d", h_b1.reg_address_array[i]);
      end
   end
endmodule

I have fill an array with different values, but have to make sure that value 25 and 50 are part of an array. I tried above code but not getting the desired results.

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

You have not connected through any constraint of reg_address to any value in reg_address_array.

The dist construct is not a hard constraint that a value in the set of ranges appear, only that values not in the ranges do not appear.

Maybe this helps

class base_b1;
    rand bit [31:0] reg_address_array[$];
 
   constraint b1_c {
      foreach (reg_address_array[i])
         reg_address_array[i] dist {5:/25, 10:/25, [25:50]:/50};
      reg_address_array.or() with (item==25); // 25 must appear at least once
      reg_address_array.or() with (item==50); // 50 must appear at least once
   }
   constraint size_c2 { 
      reg_address_array.size inside {[9:10]};
   }
endclass: base_b1

In reply to dave_59:

In reply to MarshallX:
Please use code tags making your code easier to read. I have added them for you.
You have not connected through any constraint of reg_address to any value in reg_address_array.
The dist construct is not a hard constraint that a value in the set of ranges appear, only that values not in the ranges do not appear.
Maybe this helps

class base_b1;
rand bit [31:0] reg_address_array[$];
constraint b1_c {
foreach (reg_address_array[i])
reg_address_array[i] dist {5:/25, 10:/25, [25:50]:/50};
reg_address_array.or() with (item==25); // 25 must appear at least once
reg_address_array.or() with (item==50); // 50 must appear at least once
}
constraint size_c2 { 
reg_address_array.size inside {[9:10]};
}
endclass: base_b1
//-----------------------------------------------
//--------------- Inheritence Examples ----------
//-----------------------------------------------
class base_b1;

   rand bit [31:0] reg_address_array[$];

   constraint size_c2 { 
      reg_address_array.size inside {[9:10]};
   }

   constraint address_c1 {
      foreach (reg_address_array[i])
         reg_address_array[i] dist {5:/25, 10:/25, [25:30]:/50};

      reg_address_array.or() with (item == 05);
      reg_address_array.or() with (item == 10);
   }
 
   virtual function display_var();
      for (int i=0; i<reg_address_array.size(); i++) begin
         $display("This is base class !! and reg_address is %0d", reg_address_array[i]);
      end
   endfunction: display_var
endclass: base_b1 

//----------------------------------
//------------ module top ----------
//----------------------------------
module tb_top;

   //------------------------------------------------------
   //--- handle declaration must be outside "initial begin"
   //------------------------------------------------------
   base_b1 h_b1;

   initial begin
      $display("This is module top !!");

      h_b1 = new();

      assert(h_b1.randomize());
      h_b1.display_var();
   end
endmodule

Thanks much dave, the code is working, but have a question why (.or) operation used? The (dist) operator is generating the required values.

In reply to MarshallX:

The dist operator only gives you a probability of generating the required values, it does not guarantee them. In your example, your gave the value 5 a 25% chance of being picked. That means if you pick 10 values, there is a (.75)10 or 5.6% chance of the value 5 not being picked for any of the array element values. The .or() method forces 5 to be one of the picked values.

Try removing the .or() constraints and randomizing 100 times. (either by using 100 different seeds, or putting randomize in a repeat (100) loop)