-2

I need to generate odd numbers after randomizing elements of an array in UVM sequence .

Grace90
  • 205
  • 3
  • 19

1 Answers1

2

Let's say you have the following class with an array variable:

class some_class;

  rand int array[10];

endclass

If you want to constrain each element of the array, you can use the foreach construct. To get all even numbers in the array, you can constrain each element of the array to be odd. A number is odd if the remainder of division by 2 isn't 0:

class some_class;

  // ...

  constraint all_array_elems_odd {
    foreach (array[i])
      array[i] % 2 != 0;
  }

endclass

You can check that it works with the following example:

module test;

  initial begin
    automatic some_class obj = new();
    if (!obj.randomize())
      $fatal(0, "Randomization error");
    $display("%p", obj);
  end

endmodule

Note that I didn't use compare the remainder of division against 1. This is because some programming languages (SystemVerilog included) use % for the remainder operation and not the modulus operation. The remainder can be negative (whereas the modulus is always positive). Negative odd numbers will have a remainder of -1, so forcing the remainder to be 1 will force the numbers to be positive.

Tudor Timi
  • 7,453
  • 1
  • 24
  • 53