Regarding Weighted Distribution concept

My query is about weighted distribution.

class packet;
  rand bit [3:0] addr_1;
  rand bit [3:0] addr_2;

  constraint addr_1_range {   addr_1 dist { 2 := 3, [10:12] := 3 }; }
  constraint addr_2_range {   addr_2 dist { 2 :/ 5, [10:12] :/ 15 }; }
endclass

module constr_dist;
  initial begin
    packet pkt;
    pkt = new();

    $display("------------------------------------");
    repeat(12) begin
      pkt.randomize();
      $display("\taddr_1 = %0d",pkt.addr_1);
    end
    $display("------------------------------------");
    $display("------------------------------------");
    repeat(12) begin
      pkt.randomize();
      $display("\taddr_2 = %0d",pkt.addr_2);
    end
    $display("------------------------------------");
  end
endmodule

The expected output for constraint addr_1 is 2 - > 3 times,10 → 3 times,11 → 3 times,12 → 3 times.
The expected output for constraint addr_2 is 2 - > 3 times,10 → 3 times,11 → 3 times,12 → 3 times.

But the actual output is this which is contradicting the actual concept.

KERNEL: ------------------------------------

KERNEL: addr_1 = 11

KERNEL: addr_1 = 11

KERNEL: addr_1 = 12

KERNEL: addr_1 = 11

KERNEL: addr_1 = 2

KERNEL: addr_1 = 2

KERNEL: addr_1 = 10

KERNEL: addr_1 = 2

KERNEL: addr_1 = 10

KERNEL: addr_1 = 2

KERNEL: addr_1 = 11

KERNEL: addr_1 = 10

KERNEL: ------------------------------------

KERNEL: ------------------------------------

KERNEL: addr_2 = 11

KERNEL: addr_2 = 11

KERNEL: addr_2 = 11

KERNEL: addr_2 = 10

KERNEL: addr_2 = 11

KERNEL: addr_2 = 2

KERNEL: addr_2 = 11

KERNEL: addr_2 = 2

KERNEL: addr_2 = 10

KERNEL: addr_2 = 10

KERNEL: addr_2 = 2

KERNEL: addr_2 = 10

KERNEL: ------------------------------------

Use the randc modifier instead of the rand modifier and you will get the results you are seeking. The rand modifier is randomization with replacement, so you would need to run the repeat loop many times to approach the distribution weights.

In reply to sbellock:

randc would only work here because the distribution is even.

Please see logic to determine the number of times an item to be repeated in a distribution | Verification Academy

In reply to dave_59:

Hi Dave ,

Won’t that be an Error ? . Since randc can’t be used within dist

Also I have a few queries regarding “dist”

(a) Can I expect Values ( 0 , 1 , 3 - 9 , 13 - 15 ) which are not specified in the
constraints since they would have a default weight of 1 ? . It doesn’t appear in above
Output

(b) LRM says dist requires the variable to be rand . So can it be used in std::randomize()
OR **Object.randomize( Non_Random_Class_Property )**since we don’t have rand variables
declared there ?

Regards,
AGIS

In reply to Etrx91:

Hey I learn something new everyday. In 1800-2017 section 18.5.4:

A dist operation shall not be applied to randc variables.

Thanks Etrx91!

In reply to Etrx91:
Correct. I meant randc gives an exact even distribution without using dist.

The default weight of 1 only applies to values specified. Values not in the set, and values with a weight of 0 are hard constraints.

I think the intent of that rule about rand and randc is that a variable has to be a “free” random variable to get the effect of the dist construct.