Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Random number generation

1,863 views
Skip to first unread message

K Sridhar

unread,
Feb 3, 2003, 3:52:24 AM2/3/03
to ksri...@india.hp.com
Hi,

I want to generate random numbers. If you use in a loop :
i = $random; it will generate random numbers
starting with a SEED = 0. So, for every run, I get
the same numbers in same order.

If I use i = $random(some_seed) in a loop. Then, everytime
it is called, it generates the first random number from
"some_seed".

I tried the below code where I used an initial call
with a seed and other calls were $random without argument.
But, irrespective of the seed, I am seeing the same
values in the same order for every run.


module test;
integer i;

initial
begin
i = 0;
$random(500);
end
always
begin
$display("%d", $random);
if (i < 10) i = i + 1;
else wait (i > 10);
end
endmodule

What am I missing here. I use MODELSIM.

regards

Sridhar

Al Provist

unread,
Feb 3, 2003, 3:11:36 PM2/3/03
to
K Sridhar wrote:

Hi,
Generaly, the random function uses the same order of generation. You can only specify the first number of this order with the SEED
parameter. To specify different first SEED between two run, you can use a "define" value with a init random number like Unix command date:

module test;
integer i;

initial
begin
i = 0;

$random(`INIT_SEED);


end
always
begin
$display("%d", $random);
if (i < 10) i = i + 1;
else wait (i > 10);
end
endmodule

with the unix command:

verilog +define+INIT_SEED=`date +%s`

regards

Jerry

unread,
Feb 3, 2003, 7:15:50 PM2/3/03
to
K Sridhar <ksri...@india.hp.com> wrote in message news:<3E3E2DC8...@india.hp.com>...

> What am I missing here. I use MODELSIM.
>
> regards
>
> Sridhar
Sridhar,

I'm surprized that your code compiled, because your use
of $random(500) is incorrect - argument of this system
function must be a register (integer, time, reg vector).
If you are calling $random without an argument, then you
are always getting the same sequence. If you want to have
control over the sequence, you must call random with
the same register parameter each time; the function will
update the register in addition to generating random number.
The only assignment to seed should be initial value assignment.
Try this:
module test;
integer i, seed;

initial
begin
i = 0;

seed = 500; // modify this to see different numbers
end
always
begin
$display("%d", $random(seed));


if (i < 10) i = i + 1;
else wait (i > 10);
end
endmodule

Good luck,

Jerry

Jie Ding

unread,
Feb 3, 2003, 9:36:41 PM2/3/03
to
K Sridhar wrote:

You can use current time as seed, if you want to get a different sequence
automatically each time. But make sure that is what you want, because using
different seed each run can really complicate things.

Jie

Steven Sharp

unread,
Feb 4, 2003, 7:00:19 PM2/4/03
to
Jerry is correct. The "seed" is not a starting value for subsequent calls
to $random() without an argument. It is a variable that holds the current
state of a random number sequence, which is gotten by calling $random
repeatedly with that variable. Each call produces the next number in the
sequence and modifies the variable to a new state.

You can get the result you want by initializing the variable with a random
value (e.g. by getting the value of the system clock, or inputting a value
on the command line), and then passing that variable every time you call
$random.

This has no effect on $random called without an argument. In that case,
Verilog has a hidden variable that is stepping through a sequence in
the same way.

This mechanism is much more powerful than just supplying an initial value
to a random number generator. It allows you to have a module that uses
a random sequence that is not affected by any $random calls that might
be made elsewhere in the system. If someone changes something elsewhere
in the system and your part changes behavior, you know that it is due to
changes to the inputs they are giving you, not because they have disturbed
your previous random number sequence by calling $random themselves.

Alexander Gnusin

unread,
Feb 4, 2003, 7:39:54 PM2/4/03
to
>
> You can use current time as seed, if you want to get a different sequence
> automatically each time. But make sure that is what you want, because using
> different seed each run can really complicate things.
>
> Jie

Completely agree. I would recommend to use Al Provist's example taking
unix time as random seed, but then printing this seed into the log
file. Otherwise it will be extremely difficult to recreate test cases
with bugs.

Regards,
Alex

0 new messages