I am confused between these three functions and I was wondering for some explanation. If I set the range how do I make the range exclusive or inclusive? Are the ranges inclusive or exclusive if I don't specify the range?
In addition to the answer from @dave_59, there are other important differences:
i) $random
returns a signed 32-bit integer; $urandom
and $urandom_range
return unsigned 32-bit integers.
ii) The random number generator for $random
is specified in IEEE Std 1800-2012. With the same seed you will get exactly the same sequence of random numbers in any SystemVerilog simulator. That is not the case for $urandom
and $urandom_range
, where the design of the random number generator is up to the EDA vendor.
iii) Each thread has its own random number generator for $urandom
and $urandom_range
, whereas there is only one random number generator for $random
shared between all threads (ie only one for the entire simulation). This is really important, because having separate random number generators for each thread helps you simulation improve a property called random stability. Suppose you are using a random number generator to generate random stimulus. Suppose you find a bug and fix it. This could easily change the order in which threads (ie initial
and always
blocks) are executed. If that change changed the order in which random numbers were generated then you would never know whether the bug had gone away because you'd fixed it or because the stimulus has changed. If you have a random number generator for each thread then your testbench is far less vulnerable to such an effect - you can be far more sure that the bug has disappeared because you fixed it. That property is called random stability.
So, As @dave_59 says, you should only be using $urandom
and $urandom_range
.
You should only be using $urandom
and $urandom_range
. These two functions provide better quality random numbers and better seed initialization and stability than $random
. The range specified by $urandom_range is always inclusive.
Although $random
generates the exact same sequence of random numbers for each call, it is extremely difficult to keep the same call ordering as soon as any change is made to the design or testbench. Even more difficult when multiple threads are concurrently generating random numbers.
"If I set the range how do I make the range exclusive or inclusive?"
According to IEEE 1800-2017, $urandom_range(max, min)
specifies a range that is inclusive at both ends of the range.
Additionally: both $urandom_range(0, 3)
and $urandom_range(3, 0)
will return a random number between 0 and 3, inclusive.
© 2022 - 2024 — McMap. All rights reserved.
$urandom_range()
and$urandom()
are from SystemVerilog IEEE Std 1800-2012. Verilog is its predecessor – Mismate