Constrained Random Value in verilog

HI friends, I want to assign a random value either +5 or -5 to a port . How do i do that? I tried $random function but it allows to assgin values between -5 and

+5 or 0 to 5 . But what i want is exactly +5 or else -5. Anyone know how to do it ..

thanks whizkid

Reply to
whizkid
Loading thread data ...

you can say reg r; r = $random; if (r) out = 5; else out = -5;

hopefully the LSB of $random is as random as the rest :-)

Reply to
m

If you only need two output values you really want a single bit random function which returns 0 or 1. Then use that bit to select -5 or +5 something like: reg rval; integer seed; integer port;

forever begin seed = 1; rval = $random (seed); port = rval ? +5 : -5; . . . end

Reply to
Gabor Szakacs

my_int = ($random%2*10)-5;

-- SystemVerilog DPI tutorial on Project VeriPage:

formatting link
For subscribing to Project VeriPage mailing list:

Reply to
Swapnajit Mittra

The approach to selecting between two values is good. However, if you keep resetting seed to 1 every time before you call $random, you will get the same (nonrandom) result every time too.

Reply to
Steven Sharp

Thanks to all who replied to my post..

Reply to
whizkid

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.