Constraint to generate values divisible by 5 in ascending order

In reply to chetansahu:

A better solution:


class random_values;
  rand int unsigned values[];
  rand int unsigned value_size;

  constraint value_size_c { soft value_size == 10; } // Array size. Soft to allow override in randomize() call
  constraint size_c { values.size() == value_size; }; // Set values[] size from value_size
  constraint value_c { foreach (values[i]) values[i] < values.size() * 2;}  // Constrain to max of (2 * size of array)
  constraint unique_c { unique {values}; }; // Each value is unique
  
  function void post_randomize();
    foreach (values[i]) values[i] = values[i] * 5; // Make each value divisible by 5
    values.sort(); // Make values ascending
  endfunction
endclass

module test;
  random_values rv = new();

  initial begin
    if (!rv.randomize())
      $display("Randomization failed!");
    else
      $display("Random values: %p", rv.values);
    if (!rv.randomize() with { value_size == 20;})  // Make a bigger array
      $display("Randomization failed!");
    else
      $display("Random values: %p", rv.values);
  end
endmodule