In SystemVerilog, $urandom
and $urandom_range
are system functions used to generate pseudo-random integers. It’s especially useful in testbenches and verification environments where randomization is essential to stimulate various input scenarios for DUT (Design Under Test).
$urandom
- Returns a 32-bit unsigned random number each time it is called.
- It uses a seed to initialize the random number generator, and each call to
$urandom
produces a different value based on this seed. - If you use the same seed, the random numbers will follow the same sequence, which is useful for debugging. Without explicit seeding,
$urandom
uses an automatic seed.
Example
int rn;
rn = $urandom; // automatic seed
rn = $urandom(24); // seed with a specific values
rn = $urandom % 10; // random value between 0 and 9
$urandom_range
- The function
$urandom_range(min,max)
returns an unsigned integer within the specified range (min, max).
Example
int rn;
rn = $urandom(10,20); // generates random values between 10 and 20
Points to Remember
- Both
$urandom
and$urandom_range
functions generate only 32-bit integers. - To create a random value larger than 32 bits, you can concatenate two 32-bit random numbers.