Array randomization

i have array bit [15:0] data;
i want to randomize array 5 times such a way that whatever first value comes next value should be its incremental to that value.(incremental value can be anything 1,2,3 etc.)
If first randomized value is 20 then upcoming value should be 22,24,26,28.
How to write constraint related to this in systemverilog?

In reply to Kishan_123:

You might want to add a constraint so that the incremental value does not overflow depending on how many times you expect to call randomize.

class A;
  rand bit [15:0] data, increment;
  constraint c_incr {
    (const'(increment) != 0) -> {
      increment == const'(increment);
      data == const'(data) + increment;
    }
    increment > 0;
  }
endclass

module top;
  A a = new();
  initial repeat(10) begin
    assert(a.randomize());
    $display("%p",a);
  end
endmodule

In reply to dave_59:

Hi Dave,

I tried above code on EDA playground (VCS tool) and in VCS “const” is not part of it. so i’m getting below syntax error.

Error-[SE] Syntax error
Following verilog source has syntax error :
“testbench.sv”, 6: token is ‘const’
(const’(increment) != 0) → {
^
SystemVerilog keyword ‘const’ is not expected to be used in this context.

In reply to Kishan_123:

This is defined in section 6.24.1 Cast operator. This Mentor sponsored public forum is not for discussing tool specific issues. Please read you tool’s user manual or contact your tool vendor directly for support.

In reply to Kishan_123:

you can create a variable prev_data and use that instead of const’()


rand int data;
int prev_data; //not rand
constraint {
data == prev_data + increment;
}
function void post_randomize();
  prev_data = data;
endfunction

In reply to ssureshg_:

Hi,
In your code initial value is 0. but i want first value should be randomize and onwards values should be increment.

In your code if increment = 2; then output will be 0,2,4,6,8 … but,
i want, if first randomized value is 25 then output should be 25,27,29,31 …

In reply to Kishan_123:

The above logic was only for getting const’ functionality for simulators that does not support it yet. should apply other constraints from Dave’s code.

here you go

In reply to D SAINATH:

This code is illegal for a couple of reasons. You are not allowed to declare a method with a static lifetime inside a class—they can only have automatic lifetimes (so as not to be confused with a static class method qualifier)

And even if you explicitly declare the variable count with a static lifetime, functions used in constraints are not allowed to preserve state information (have no side-effect).