Rand_mode of a dynamic array not working

I am trying to disable randomization of a dynamic array declared as rand, by calling

obj.myarray.rand_mode(0);

Yet it still randomizes. Using rand_mode on a static width array works fine.

You can run my simple example here:
EDA Playground

You shouldn’t use assert() around a randomize call. The reason for this is that there are times that assertions may be disabled for performance. When a simulator disables assertions, everything inside the assert() statement is ignored, so randomize won’t be called. It is recommended instead to use simple if/else statements:


  // randomizing an array whose members are type randc does NOT result in an
  // array of unique values.  Each individual member is of type randc, and
  // independant of other array members.
class randc_array;
  randc bit [3:0] byte_array[];

  constraint c_size {
    byte_array.size ==4;
  }

endclass

class randc_array2;
  randc bit [3:0] byte_array[4];
endclass

module top;

  randc_array RandcArray;
  randc_array2 RandcArray2;

  initial begin

    //this doesnt work (randomizing when I told it NOT to!)
    RandcArray = new();
    RandcArray.byte_array.rand_mode(0);
    if (!RandcArray.randomize()) $display("Randomization failed!");
    else $display("%p", RandcArray);

    // this works (disabled the randomization)
    RandcArray2 = new();
    RandcArray2.byte_array.rand_mode(0);
    if (!RandcArray2.randomize()) $display("Randomization failed!");
    else $display("%p", RandcArray2);

  end
endmodule

In Questa, the first randomize() call fails, as expected. Perhaps this is a simulator error.

Loading sv_std.std

Loading work.top_sv_unit(fast)

Loading work.top(fast)

VSIM 1> run -a

Randomization failed!

‘{byte_array:’{0, 0, 0, 0}}

In reply to cgales:

I’ve tried it out in another Big Three simulator and there I also get '{ 0, 0, 0, 0 } for the first call to randomize() (i.e. it randomizes the dynamic array, but not its elements). I don’t think this is in the spirit of rand_mode(0) and agree with what Questa does (fails the randomization because the constraint can’t be met if the array isn’t randomized). Unfortunately, the standard isn’t 100% clear on what should happen in this case and it seems that other vendors interpreted it differently.

In reply to Tudor Timi:

So RivieraPro, when a size constraint is in place, will resize the array, and apply the random mode to each individual member. It seems common sense that when rand_mode(0) is used, it would apply this to each member after resizing. Tudor, as you mentioned, rand_mode(0) won’t effect array elements that don’t already exist. So the elements need to be created, then apply rand_mode(0), disable array-size constraint, and then randomize. eda playground

Questa fails the randomization call.

This “other simulator” resizes the array, but doesn’t recognize the random attribute at all. It may be strictly that the elements have to exist.

It’s interesting to see the interpretations.