Tutorials

The dist keyword is helpful whenever to have weighed distribution during randomization. The probability random value occurrence can be controlled using the dist keyword.

## How to allocate weighted distribution?

There are two ways to provide weightage for values.

1. Using :/ operator
2. Using := operator

Syntax for the operator:

``````value :/ weightage
value := weightage

//Value - Random value of the variable
//Weightage - weight for random variable i.e. occurrence of value can be specified.
``````

Note: The sum of weightage is not necessary to be 100.

## :/ operator

1. For specific value: Assign mentioned weight to that value
2. For range of values ([<range1>: <range2>]):  Assigns weight/(number of value) to each value in that range

### :/ operator Example

``````constraint value_c {value dist {3:/4, [5:8] :/ 7}; }

//Elaboration:
value = 3. weightage = 4

value = 5. weightage = 7/4 = 1.75
value = 6. weightage = 7/4 = 1.75
value = 7. weightage = 7/4 = 1.75
value = 8. weightage = 7/4 = 1.75
``````

## := operator

For a specific value or range of value, the mentioned weight is assigned.

### := operator Example

``````constraint value_c {value dist {3:=4, [5:8] := 7}; }

//Elaboration:
value = 3. weightage = 4

value = 5. weightage = 7
value = 6. weightage = 7
value = 7. weightage = 7
value = 8. weightage = 7
``````

## :/ and := Operator Example

In the below example, both operators are used. Notice how the occurrence of values is changed.

``````class seq_item;
rand bit [7:0] value1;
rand bit [7:0] value2;

constraint value1_c {value1 dist {3:/4, [5:8] :/ 7}; }
constraint value2_c {value2 dist {3:=4, [5:8] := 7}; }

endclass

module constraint_example;
seq_item item;

initial begin
item = new();

repeat(5) begin
item.randomize();
\$display("value1 (with :/) = %0d, value2 (with :=)= %0d", item.value1, item.value2);
end
end
endmodule``````

Output:

``````value1 (with :/) = 3, value2 (with :=)= 8
value1 (with :/) = 5, value2 (with :=)= 5
value1 (with :/) = 3, value2 (with :=)= 7
value1 (with :/) = 3, value2 (with :=)= 6
value1 (with :/) = 3, value2 (with :=)= 6``````

System Verilog Tutorials