User Tools

Site Tools


Ho do I generate different random numbers when re-running one of the random number generators?

Q: I tried to generate some random numbers by using uniform(x,y), but I got the same “random” numbers every time. My code is attached below.

       A(i)= uniform(0,1);

The random numbers are generated using a deterministic algorithm (called a generator) inside GAMS to ensure reproducible runs. If you want different numbers on different runs, you must reseed the generator (by setting execseed). The value of execseed has to be between 0.0 and 10**9. You may use something like:

set i /i1*i10/;
parameter A(i);
execseed = 1e8*(frac(jnow));
     A(i) = uniform(0,1);
option decimals=0;display A;

Running this model twice returns:

----      7 PARAMETER A  

i1  9.284681E-1,    i2  6.635613E-1,    i3  5.481121E-1,    i4  8.099392E-1
i5  6.981181E-1,    i6  3.511729E-1,    i7  8.274803E-1,    i8  1.329265E-1
i9  5.253862E-1,    i10 5.751743E-1


----      7 PARAMETER A  

i1  7.756601E-1,    i2  8.195542E-2,    i3  1.093455E-1,    i4  6.673705E-1
i5  3.787748E-2,    i6  3.917081E-1,    i7  6.981375E-1,    i8  5.950382E-2
i9  9.432999E-1,    i10 6.857027E-1

Do not update the seed within the loop! There is no need for doing that and the value of jnow may not be updated for each element of the loop (and thus execseed may not change). The following code illustrates this:

Scalar old, new, count;
new = jnow;
old = new;
count = 0;
  count = count + 1;
  new   = jnow;
Display count;

The outcome depends on the speed of your PC, but on a current machine it goes through the While loop 216 times before jnow changes:

----      9 PARAMETER count                =      216.000  
gams/generate_different_random_numbers_when_re-running_one_of_the_random_number_generators.txt · Last modified: 2008/06/26 14:17 by support