Constrained Random Value in verilog

W

whizkid

Guest
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
 
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

whizkid@gamebox.net (whizkid) wrote in message news:<edfc084c.0410180647.4b8159a1@posting.google.com>...
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
 
whizkid@gamebox.net (whizkid) wrote in message news:<edfc084c.0410180647.4b8159a1@posting.google.com>...
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
my_int = ($random%2*10)-5;

--
SystemVerilog DPI tutorial on Project VeriPage:
http://www.project-veripage.com/dpi_tutorial_1.php
For subscribing to Project VeriPage mailing list:
<URL: http://www.project-veripage.com/list/?p=subscribe&id=1>
 
gabor@alacron.com (Gabor Szakacs) wrote in message news:<8a436ba2.0410181001.2f80854a@posting.google.com>...
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
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.
 

Welcome to EDABoard.com

Sponsor

Back
Top