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?
$urandom( ) The system function $urandom provides a mechanism for generating pseudorandom numbers. The function returns a new 32-bit random number each time it is called. The number shall be unsigned. variable = $urandom(seed); The seed is an optional argument that determines the sequence of random numbers generated.
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.
R = $random % 2; Above example will generate random value between -1 to 1. If only positive is needed, use concatenation operator as follows.
urandom() method is used to generate a string of size random bytes suitable for cryptographic use or we can say this method generates a string containing random characters. Return Value: This method returns a string which represents random bytes suitable for cryptographic use.
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 you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With