Source – SV Verification Directory
The inside
operator lets you create sets of values from which a variable can randomly be selected. By default, all values are chosen with equal probability, unless other constraints are applied. You can also include variables in these sets to make the randomization more dynamic.
Syntax
rand int rn;
constraint set_value {rn inside {1,2,3,4};} // randomly selects from set of values
constraint set_value {rn inside {10:20};} // randomly selects values from 10 to 20
constraint set_value {!(rn inside {10:20});} // randomly selects values outside of the range 10 to 20
Example Code
class inside_op;
rand bit [3:0] a;
rand bit [3:0] b;
rand bit [3:0] c;
constraint const_a {a inside {1,2,3,4};}
constraint const_b {b inside {[10:15]};}
constraint const_c {!(c inside {[10:15]});}
function void disp();
$display("a = %0d, b = %0d, c = %0d", a, b, c);
endfunction // disp
endclass // inside_op
module tb_const_inside_op;
inside_op i_op;
initial begin
i_op = new();
for(int i = 0; i < 16; i++) begin
assert(i_op.randomize())
else
$error("Randomization Failed ;(");
i_op.disp();
end
end
endmodule