Randomization – constrain values uniquely

Did you know ...

A simple way to allot unique values to random variables is described below :

bit [7:0] masterId [6];

rand bit [3:0] number;

function void pre_randomize ();

   foreach (masterId [i]) begin

         masterId [i] = i;

   end

endfunction

function void post_randomize ();

    repeat (number) masterId.shuffle;

endfunction


 

I know it looks dumb, but this is an easy way out. You can substitute i with another array with valid values like

bit [7:0] array [6] = ‘{8’h4e, 8’h5a, 8’hf1, 8’he3, 8’h4, 8’hbb};

function void pre_randomize ();

   foreach (masterId [i]) begin

       masterId [i] = array [i]; 

   end

endfunction