Sometimes we come across scenarios where we want the solver to randomly pick one out of the many statements. The keyword `randcase` introduces a `case` statement that randomly selects one of its branches. The case item expressions are positive integer values that represent the weights associated with each item. Probability of selecting an item is derived by the division of that item's weight divided by the sum of all weights.

## Syntax

```  ```

randcase
item 	: 	statement;
...
endcase

```
```

## Example

The sum of all weights is 9, and hence the probability of taking the first branch is 1/9 or 11.11%, the probability of taking the second branch is 5/9 or 55.56% and the probability of taking the last branch is 3/9 or 33.33%.

```  ```

module tb;
initial begin
for (int i = 0; i < 10; i++)
randcase
1 	: 	\$display ("Wt 1");
5 	: 	\$display ("Wt 5");
3 	: 	\$display ("Wt 3");
endcase
end
endmodule

```
```

Note that 5 appeared maximum number of times, while 1 appeared least number of times and 3 somewhere in between.

Simulation Log
```ncsim> run
Wt 5
Wt 5
Wt 3
Wt 5
Wt 1
Wt 3
Wt 5
Wt 3
Wt 3
Wt 5
ncsim: *W,RNQUIE: Simulation is complete.

```

If a branch specifies a zero weight, then that branch is not taken.

```  ```

module tb;
initial begin
for (int i = 0; i < 10; i++)
randcase
0 	: 	\$display ("Wt 1");
5 	: 	\$display ("Wt 5");
3 	: 	\$display ("Wt 3");
endcase
end
endmodule

```
```
Simulation Log
```ncsim> run
Wt 5
Wt 5
Wt 3
Wt 5
Wt 5
Wt 3
Wt 5
Wt 3
Wt 3
Wt 5
ncsim: *W,RNQUIE: Simulation is complete.

```

If all randcase_items specify zero weights, even though it doesn't make any sense to do so, then no branch will be taken and might result in a run-time warning.

```  ```

module tb;
initial begin
for (int i = 0; i < 10; i++)
randcase
0 	: 	\$display ("Wt 1");
0 	: 	\$display ("Wt 5");
0 	: 	\$display ("Wt 3");
endcase
end
endmodule

```
```
Simulation Log
```ncsim> run
ncsim: *W,RANDNOB: The sum of the weight expressions in the randcase statement is 0.
No randcase branch was taken.
File: ./testbench.sv, line = 4, pos = 14
Scope: tb.unmblk1
Time: 0 FS + 0

ncsim: *W,RANDNOB: The sum of the weight expressions in the randcase statement is 0.
No randcase branch was taken.
File: ./testbench.sv, line = 4, pos = 14
Scope: tb.unmblk1
Time: 0 FS + 0

ncsim: *W,RANDNOB: The sum of the weight expressions in the randcase statement is 0.
No randcase branch was taken.
File: ./testbench.sv, line = 4, pos = 14
Scope: tb.unmblk1
Time: 0 FS + 0

...

```

Each call to `randcase` retrieves one random number in the range of 0 to the sum of the weights. The weights are then selected in declaration order: small random numbers correspond to the first (top) weight statements.