How can an array element can be randomized to particular value by using inline constraint?


Hi,

I have a doubt on randomization of array elements with a particular value by using inline constraint.

For ex:-
class A
rand bit[127:0]array[256];
endclass

I want to make most of element’s value of an array as ‘0’ and other elements values can be randomized.

How can do the above operation by using inline constraint.

May i know which approach is correct?

this.randomize(array) with {foreach(array[i])
array[i] dist{'0:=90, [1:((2**(128)-1]):=10};};

(or)

foreach(array[i]) begin
this.randomize(array) with {array[i] dist{'0:=90, [1:((2**(128)-1]):=10};};

(or)

is there any approach?

Your first method should work. The second will not give you the desired result because all elements are randomized, but only one element is constrained each time through the loop.

You can also use std::randomize for any variable or element of an array (does not even need to be a rand class property).

  foreach (array[i])
	std::randomize(array[i]) with {array[i] dist {'0:=90,[1:'1]:/10};};

Notes:

  • You want :/10 not :=10 for the probability. Otherwise there is more probability of having a non-zero value because each individual value in the range is given a weight of 10.
  • You can use '1 for all ones.