Constrained Random Value in verilog

Oct 18, 2004 5 Replies

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


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 :-)

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

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

-- SystemVerilog DPI tutorial on Project VeriPage:

formatting link
For subscribing to Project VeriPage mailing list:

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.

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required