How do you generate random numbers within a range of values in SystemVeri…
How do you generate random numbers within a range of values in SystemVerilog?
Model answer
There are several ways. Each example below gives a value from 40 to 50, inclusive. Constraint in a class: use the inside operator with a range. Scope randomize: use std::randomize() with an inline constraint, without needing a class. System function: use the urandom_range system function, which takes the maximum and then the minimum. With modulo, the formula is MIN + {$random} % (MAX - MIN + 1). The concatenation makes the value unsigned, and the + 1 includes the maximum.