# dist Constraint in SystemVerilog

Constraint provides control on randomization, from which the user can control the values on randomization. it would be good if it’s possible to control the occurrence or repetition of the same value on randomization.yes its possible, with dist operator, some values can be allocated more often to a random variable. this is called a weighted distribution. dist is an operator, it takes a list of values and weights, separated by := or :/ operator.

## weighted distribution

As the name says, in weighted distribution weight will be specified to the values inside the constraint block. Value with the more weight will get allocated more often to a random variable.

### syntax

```value := weight   or
value :/ weight
```

Value   – desired value to a random variable
weight – indicates how often the value needs to be considered on randomization

• The values and weights can be constants or variables,
• value can be single or a range
• the default weight of an unspecified value is := 1
• the sum of weights need not be a 100

The := operator assigns the specified weight to the item, or if the item is a range, specified weight to every value in the range.

```addr dist { 2 := 5, [10:12] := 8 };

for addr == 2 , weight 5
addr == 10, weight 8
addr == 11, weight 8
addr == 12, weight 8
```

The :/ operator assigns the specified weight to the item, or if the item is a range, specified weight/n to every value in the range. where n is the number of values in the range.

```addr dist { 2 :/ 5, [10:12] :/ 8 };

for addr == 2 , weight 5
addr == 10, weight 8/3
addr == 11, weight 8/3
addr == 12, weight 8/3
```

### weighted distribution constraint examples

#### randomization with dist operator

In the example below,
On randomization, the possibility of ‘addr’ is getting the value of 10 is more than 7 and 2. this is because of weight specified to get value 10 is more than the other two values.

```class packet;
rand bit [3:0] addr;

constraint addr_range { addr dist { 2 := 5, 7 := 8, 10 := 12 }; }
endclass

module constr_dist;
initial begin
packet pkt;
pkt = new();
\$display("------------------------------------");
repeat(10) begin
pkt.randomize();
end
\$display("------------------------------------");
end
endmodule```

Simulator Output

```------------------------------------
------------------------------------```

Click to execute on #### difference between := and :/ dist operator

In the below example,

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

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

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

\$display("------------------------------------");
repeat(10) begin
pkt.randomize();
end
\$display("------------------------------------");
\$display("------------------------------------");
repeat(10) begin
pkt.randomize();
end
\$display("------------------------------------");
end
endmodule```

Simulator Output

```------------------------------------
------------------------------------
------------------------------------
Click to execute on
{"ft_lang_label":"__label__en","ft_lang_prob":0.6142321,"math_prob":0.98407996,"size":3709,"snap":"2022-40-2023-06","text_gpt3_token_len":1003,"char_repetition_ratio":0.26558703,"word_repetition_ratio":0.2538071,"special_character_ratio":0.42760852,"punctuation_ratio":0.17018072,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98384535,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-10-05T17:51:10Z\",\"WARC-Record-ID\":\"<urn:uuid:cde8036c-79c8-418e-b23f-ceb239719c65>\",\"Content-Length\":\"87345\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:df5c37eb-6412-405a-8fa9-09dc93159054>\",\"WARC-Concurrent-To\":\"<urn:uuid:2be07cee-120c-4005-a27d-e5d0cb4a7e2a>\",\"WARC-IP-Address\":\"35.175.60.16\",\"WARC-Target-URI\":\"https://verificationguide.com/systemverilog/systemverilog-distribution-constraint/\",\"WARC-Payload-Digest\":\"sha1:4D27A7TRT72QJVRZR2LNHEWB2N7GTI66\",\"WARC-Block-Digest\":\"sha1:UDMGWHZHJ5QYJW4D3MAU63MYGGRHP7QQ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-40/CC-MAIN-2022-40_segments_1664030337663.75_warc_CC-MAIN-20221005172112-20221005202112-00018.warc.gz\"}"}