Generate Same Sequence of Random Numbers in IDL and C [message #89061] |
Mon, 21 July 2014 09:11  |
sweiss1993
Messages: 4 Registered: July 2014
|
Junior Member |
|
|
I am in the process of developing a C version of a program originally written in IDL. The programs generate arrays with random number generators. I use gsl_rng_uniform and gsl_ran_poisson in the C version, and both random number generators use the gsl_rng_mt19937 algorithm. From attempting to read GDL's source code, I noticed GDL happens to use the same functions.
I would like to compare the two versions of the program by running them with the same fixed sequence of random numbers. The rng parts of the programs are in a loop, so a new array is generated with each iteration. I would like a different set of random numbers for each iteration. So, each iteration has a different set of generated numbers, but the i-th iteration in the programs should match each other.
I could not figure out exactly how GDL sets the seed by reading the source code. Would someone be so kind as to show me how to seed the rng functions in IDL and C such that the above conditions are met? Thanks!
|
|
|
Re: Generate Same Sequence of Random Numbers in IDL and C [message #89063 is a reply to message #89061] |
Mon, 21 July 2014 10:16   |
Michael Galloy
Messages: 1114 Registered: April 2006
|
Senior Member |
|
|
On 7/21/14, 10:11 AM, sweiss1993@gmail.com wrote:
> I am in the process of developing a C version of a program originally
> written in IDL. The programs generate arrays with random number
> generators. I use gsl_rng_uniform and gsl_ran_poisson in the C
> version, and both random number generators use the gsl_rng_mt19937
> algorithm. From attempting to read GDL's source code, I noticed GDL
> happens to use the same functions.
>
> I would like to compare the two versions of the program by running
> them with the same fixed sequence of random numbers. The rng parts of
> the programs are in a loop, so a new array is generated with each
> iteration. I would like a different set of random numbers for each
> iteration. So, each iteration has a different set of generated
> numbers, but the i-th iteration in the programs should match each
> other.
>
> I could not figure out exactly how GDL sets the seed by reading the
> source code. Would someone be so kind as to show me how to seed the
> rng functions in IDL and C such that the above conditions are met?
> Thanks!
>
In IDL, you specify the seed when calling the random number generator
function, i.e.,
IDL> r = randomu(seed, 1000)
generates 1000 uniformly distributed random numbers. If "seed" is
undefined, it is created from the system clock. I haven't used GSL
random numbers, but it looks like this is how to set the seed for them:
void gsl_rng_set(const gsl_rng *r, unsigned long int seed);
Mike
--
Michael Galloy
www.michaelgalloy.com
Modern IDL: A Guide to IDL Programming (http://modernidl.idldev.com)
Research Mathematician
Tech-X Corporation
|
|
|
Re: Generate Same Sequence of Random Numbers in IDL and C [message #89071 is a reply to message #89063] |
Tue, 22 July 2014 09:21   |
sweiss1993
Messages: 4 Registered: July 2014
|
Junior Member |
|
|
Hi Mike,
Thanks for the reply! I seeded the way you described, and there is still a difference in the random number output for each program. To be specific, I seeded both with a constant integer of 4357 (default seed for GSL's RNG). I also made sure the seed was not reinitialized with every loop iteration. Both programs now have a constant output every time the program runs. However, the outputs from the programs do not match each other. On closer inspection, I noticed that the first random numbers match, but after the next iteration, they are not the same.
Since both programs use the same number generator and start with the same seed, I am guessing there must be a difference in how GDL changes the seed with each call. So, do you or anyone happen to know how exactly IDL/GDL changes the seed for a constant seed input? I have read the documentation, and attempted to read the GDL C++ source code, but neither have been much help in this regard.
- Sam
|
|
|
Re: Generate Same Sequence of Random Numbers in IDL and C [message #89072 is a reply to message #89071] |
Tue, 22 July 2014 11:07   |
Michael Galloy
Messages: 1114 Registered: April 2006
|
Senior Member |
|
|
On 7/22/14, 10:21 AM, sweiss1993@gmail.com wrote:
> Hi Mike,
>
> Thanks for the reply! I seeded the way you described, and there is
> still a difference in the random number output for each program. To
> be specific, I seeded both with a constant integer of 4357 (default
> seed for GSL's RNG). I also made sure the seed was not reinitialized
> with every loop iteration. Both programs now have a constant output
> every time the program runs. However, the outputs from the programs
> do not match each other. On closer inspection, I noticed that the
> first random numbers match, but after the next iteration, they are
> not the same.
>
> Since both programs use the same number generator and start with the
> same seed, I am guessing there must be a difference in how GDL
> changes the seed with each call. So, do you or anyone happen to know
> how exactly IDL/GDL changes the seed for a constant seed input? I
> have read the documentation, and attempted to read the GDL C++ source
> code, but neither have been much help in this regard.
>
> - Sam
>
I get the same values:
IDL> mg_gsl_rng_test
% Compiled module: MG_GSL_RNG_TEST.
% Loaded DLM: MG_GSL.
0.12696983
0.51491326
0.96671784
0.89812542
0.26047601
0.70582012
0.89723652
0.77882970
0.37674972
0.93162251
0.126970 0.514913 0.966718 0.898125 0.260476
0.705820 0.897237 0.778830 0.376750 0.931623
Here is the source code for the test:
pro mg_gsl_rng_test
compile_opt strictarr
n = 10L
original_seed = 123456ULL
seed = original_seed
mg_gsl_rng_env_setup
t = mg_gsl_rng_mt19937()
r = mg_gsl_rng_alloc(t)
mg_gsl_rng_set, r, seed
for i = 0L, n - 1L do begin
print, mg_gsl_rng_uniform(r)
endfor
seed = original_seed
print, randomu(seed, n)
end
I tested generating the values one at a time in IDL as well and it is
still the same.
The bindings to call the GSL RNG functions, i.e., mg_gsl_rng_env_setup,
mg_gsl_rng_mt19937, mg_gsl_rng_alloc, mg_gsl_rng_set, and
mg_gsl_rng_uniform are in my library:
http://github.com/mgalloy/mglib
Mike
--
Michael Galloy
www.michaelgalloy.com
Modern IDL: A Guide to IDL Programming (http://modernidl.idldev.com)
Research Mathematician
Tech-X Corporation
|
|
|
|
|
|
|
Re: Generate Same Sequence of Random Numbers in IDL and C [message #89103 is a reply to message #89091] |
Thu, 24 July 2014 10:34  |
Michael Galloy
Messages: 1114 Registered: April 2006
|
Senior Member |
|
|
On 7/23/14, 5:42 PM, sweiss1993@gmail.com wrote:
> Did you try running both of the programs I posted? For me, the first
> trials of each program yield the same sequence of randoms. Each
> subsequent trial does not match the one from the other program.
I did not run your C programs; I used my IDL bindings to GSL.
Even the first trial of poisson values were the same?
> I only have IDL on a computer in my university, and I don't have
> access to it over the summer. Unfortunately, I can only run IDL
> programs through GDL for now. That being said, I can't try out mglib
> until later this year, so I have to rely on my C version for
> comparison.
>
> It might be that you can get the same numbers for a uniform
> distribution in IDL as the C version, but not with GDL (just a
> guess). Perhaps there is a problem with my C version, but I can't
> test that well until I solve this problem.
Mike
--
Michael Galloy
www.michaelgalloy.com
Modern IDL: A Guide to IDL Programming (http://modernidl.idldev.com)
Research Mathematician
Tech-X Corporation
|
|
|