Distributed weightage constraint

Hello,

I need to randomize a variable with equal distribution between {1 , [2:5], [6:15]}
ie, probability to generate 1 is 1/3
probability to generate 2…5 is 1/3
probability to generate 6…15 is 1/3

How do i define this in sv?

Below is the code i tried, but the distribution were not same for different seeds.

class constraint_c;

  rand bit[3:0]  ack_delay;

  constraint ack_delay_constr { ack_delay dist {1 := 1, [2:5] :/ 1, [6:15] :/ 1}; }

  function void post_randomize();
    $display("ack_delay %d", ack_delay);
  endfunction
endclass

module test_constraint();

  constraint_c   cl;

  initial
  begin
    cl = new();
    repeat (6) 
    begin
      cl.randomize();
    end

    $finish();
  end

endmodule

Regards, Hani

Hello John,
For equal distribution for the above three ranges 1, [2:5] , [6:15] you have to use constraint as

“ack_delay_constr { ack_delay dist {1 := 1, [2:5] :/ 4, [6:15] :/ 10}; }”

For getting stable distribution you have to use the same seed. And the distribution will be almost same in the long repeats, but not for the small repeats. For different seeds the random distribution order will be different, but for the same seed distribution order will be same.

Regards,
Sasi

In reply to Ch Sasikanth :

The original example was modeled correctly. :/ applies the weight to the range as a whole. The constraint that Sasi wrote would give a uniform distribution to each value in the range.

For a large enough number of randomization calls, it should not matter if the seed is the same or different. How many calls did you make?

In reply to dave_59:

The program was with 6 iterations. If i increase the samples, weighted distribution works better.

The example posted can be considered as a testcase, that is required to cover 3 sets.
So i defined the testcase to randomize 2*(number of sets)times.

If i consider randc,
randc bit [2:0] cnt;
“cnt” is guaranteed to have different randomized value, and 1 testcase with 8 iterations of randomization is going to cover all the possibilities.

Using weighted distribution, How do we come up with a similar figure?

In reply to Hani John Poly:

Apparently, you slept through your Probabilities and Statistics class, and you don’t gamble. Try flipping a coin 20 times and see how often it lands on the same side consecutively. A distribution is not like a hard constraint; it is only after a large number of iterations that your solution approaches the desired distribution.

If you have a hard constraint that a number from each of the three sets it picked in a cycle, then you cannot use the dist constraint. You can use the cyclic randc constraint to select between each set:

randc bit [2:0] delay_set;
rand  bit[3:0]  ack_delay;
 
constraint delay_set_limit { delay_set < 3 }
constraint ack_delay_constr { 
   delay_set==0 -> ack_delay ==1;
   delay_set==1 -> ack_delay inside {[2:5]};
   delay_set==2 -> ack_delay inside {[6:15]};
}

This gives you an exact even selection of each set in one cycle. If want multiple selection of sets in a cycle, or don’t want the selections to be even increase the delay_set_limit:

randc bit [2:0] delay_set;
rand  bit[3:0]  ack_delay;
 
constraint delay_set_limit { delay_set < 8 }
constraint ack_delay_constr { 
   delay_set < 4 -> ack_delay==1;
   (delay_set==4 || delay_set==5) -> ack_delay  inside {[2:5]};
   delay_set inside {[6:7]} -> ack_delay  inside {[6:15]};
}

This gives you 4 selections of set 1, 2 selections of set 2:5, and 2 selections of set 6:15 in a cycle.

In reply to dave_59:

Good guess! :)
Thanks Dave.

In reply to dave_59:

Hi Dave,

Is ‘dist inside’ only supported in Questa? I can’t find anything like this in the LRM.

Thanks

In reply to laureen.giac:

That was a typo. I’m surprised it took this long for someone to catch it. Thanks. I have removed ‘dist’ from the example.

randc bit [2:0] delay_set;
rand  bit[3:0]  ack_delay;
constraint delay_set_limit { delay_set < 3 }
constraint ack_delay_constr { 
delay_set==0 -> ack_delay ==1;
delay_set==1 -> ack_delay inside {[2:5]};
delay_set==2 -> ack_delay inside {[6:15]};
}

Hello Dave, don’t you need solve delay_set before ack_delay to get an uniform distribution of
0 = 1/3, 1 = 1/3, 2 = 1/3

In reply to srshan_rocks:

No, because randc guarantees a uniform distribution of that random variable.