$random - bug?

M

Mark McDougall

Guest
Hi,

I'm attempting to generate the same sequence of pseudo-random numbers
twice - once to create a test data set and a second time to verify it.

Here's what I'm doing...

$random (42);
for (i=0; i<7; i=i+1)
data1 = $random;
$random (42)
for (i=0; i<7; i=i+1)
data2 = $random;

If I print the return value from $random(42), in both cases it is the
same value.

However, the sequence assigned to data2 does not match the sequence
assigned to data1.

What am I doing wrong? It's the sort of thing I do in C all the time. Do
I misunderstand how the $random system task is supposed to work?

Regards,
Mark
 
Mark McDougall wrote:

OK, this works...

data1 = $random (42);
for (i=0; i<7; i=i+1)
data1 = $random (data1);
data2 = $random (42);
for (i=0; i<7; i=i+1)
data2 = $random (data2);

It would seem that in order to generate a detmerinistic pseudo-random
sequence, you need to seed it with the last value - yes?

Regards,
Mark
 
Mark McDougall wrote:

It would seem that in order to generate a detmerinistic pseudo-random
sequence, you need to seed it with the last value - yes?
OK, my colleague finally found some (adequate) doco for $random on the
net - something which I had failed despite 30 mins of searching. :(

I see what I was doing wrong now...

For the benefit of others,
<http://www-ee.eng.hawaii.edu/~msmith/ASICs/HTML/Verilog/LRM/HTML/14/ch14.a.htm>

Regards,
Mark
 
Yes, you misunderstand how the "seed" argument to the $random system
task works. It is not really a seed at all, and does not work like the
C functions you may be used to.

It is not an input argument to seed the shared hidden state of the
random number sequence. It is an inout argument (which must therefore
be a variable), which is the entire state for a separate random number
sequence. It provides the current state, which the generator uses to
produce a value, and then the generator computes the next state, which
is copied back out to the variable. Then you can pass the modified
variable back in to the next call to get the next number in that
sequence. This sequence is independent of any other sequence that is
using a different variable to hold its state. In particular, it has no
effect on the sequence being produced by $random with no arguments,
which is doing the same thing but using a shared hidden variable for
the state.
 

Welcome to EDABoard.com

Sponsor

Back
Top