Constraint to generate values divisible by 5 in ascending order

Hello there!
I am trying to generate 10 values which are divisible by 5 in its ascending order. I am unable to understand where I am doing it wrong. Here is the code -

class random_value;
rand bit [7:0]var1 ;
rand int i ;

constraint c1{ var1.size() inside {[5:8]}; }

constraint c2{ i > 0; i < 10; }

constraint c3{foreach (var1[i])
if( i > 0)
var1[i] > var1[i-1];
}
endclass

module abc();
random_value pkt = new();

initial begin
pkt.randomize();
end

endmodule

I am getting output - `{34, 68, 76, 77, 78, 120, 196}

In reply to Radheychin:

Please use code tags to make your code easier to read.

I’m not seeing any constraint in your code for the “divisible by 5” requirement.

In reply to Radheychin:

class random_values;
rand int values[10];

constraint c1 { foreach (values[i]) values[i] % 5 == 0; }
constraint c2 { foreach (values[i]) values[i] < values[i+1]; }

function void post_randomize();
values.sort();
endfunction
endclass

module test;
random_values rv = new();

initial begin
rv.randomize();
$display(“Random values: %p”, rv.values);
end
endmodule

In reply to Radheychin:

You’re constraining your randomisation to:

  1. Numbers divisible by 5
  2. Print in ascending order

You constrained the ascending order part but divisibility of 5 is not. Add that and you’d be fine, something like, foreach (var1[i]) var1[i] % 5 == 0.

Hope it helps… Cheers.

In reply to chetansahu:

Hello chetansahu, thanks for your code. I tried this code, and it says “index out of bounds (values[(9 + 1)]) for unpacked array of packed elements in constraint”.

In reply to Shashank Gurijala:

Thank you Shashank! I will try using this.

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

In reply to cgales:

class packet;
  rand int data[10];
  
  constraint c_data {
    foreach(data[i]) {
      data[i] % 5 == 0;
      data[i] inside {[0:1000]};
      foreach(data[j]) {
        if(j > i ) data[j] > data[i];
      }
    }
    
  }
endclass
module top;
  initial begin 
    packet p = new;
    repeat(1) begin 
      p.randomize();
      $display("%p",p.data);
    end
  end 
endmodule