comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Generate Same Sequence of Random Numbers in IDL and C
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Generate Same Sequence of Random Numbers in IDL and C [message #89061] Mon, 21 July 2014 09:11 Go to next message
sweiss1993 is currently offline  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 Go to previous messageGo to next message
Michael Galloy is currently offline  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 Go to previous messageGo to next message
sweiss1993 is currently offline  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 Go to previous messageGo to next message
Michael Galloy is currently offline  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 #89074 is a reply to message #89072] Tue, 22 July 2014 14:13 Go to previous messageGo to next message
Michael Galloy is currently offline  Michael Galloy
Messages: 1114
Registered: April 2006
Senior Member
On 7/22/14, 12:07 PM, Michael Galloy wrote:
> I get the same values:

I found an oddity that if you use the DOUBLE keyword of RANDOMU, the
numbers do no match (after the first), even though GSL's routines will
return doubles not floats.

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 #89079 is a reply to message #89072] Wed, 23 July 2014 09:52 Go to previous messageGo to next message
sweiss1993 is currently offline  sweiss1993
Messages: 4
Registered: July 2014
Junior Member
So I made a couple of test programs myself to show you the context of my problem.

IDL version:
http://pastebin.com/bKifVSgE

C version:
http://pastebin.com/T9Ujaxx1
http://pastebin.com/fLhpn6Jy
http://pastebin.com/v8dxfwB0

Make sure you have the GSL and FFTW libraries before trying to use the C version. Let me know if you have trouble getting either program to run.

If you run the programs, you'll see that there is in fact a difference in the outputs like I described.

Also I tried to configure mglib, and I got the following error:
"Could NOT find IDL (missing: IDL_LIBRARY IDL_INCLUDE_DIR)"

Is it because I have GDL and not IDL on my computer?

Thanks again,
Sam
Re: Generate Same Sequence of Random Numbers in IDL and C [message #89083 is a reply to message #89079] Wed, 23 July 2014 11:03 Go to previous messageGo to next message
Michael Galloy is currently offline  Michael Galloy
Messages: 1114
Registered: April 2006
Senior Member
On 7/23/14, 10:52 AM, sweiss1993@gmail.com wrote:
> So I made a couple of test programs myself to show you the context of
> my problem.
>
> IDL version: http://pastebin.com/bKifVSgE
>
> C version: http://pastebin.com/T9Ujaxx1 http://pastebin.com/fLhpn6Jy
> http://pastebin.com/v8dxfwB0
>
> Make sure you have the GSL and FFTW libraries before trying to use
> the C version. Let me know if you have trouble getting either program
> to run.
>
> If you run the programs, you'll see that there is in fact a
> difference in the outputs like I described.

Just doing the uniformly distributed values, I get exactly the same
values. When I do the poisson values also, I haven't been able to get
the same values.

> Also I tried to configure mglib, and I got the following error:
> "Could NOT find IDL (missing: IDL_LIBRARY IDL_INCLUDE_DIR)"
>
> Is it because I have GDL and not IDL on my computer?

Definitely need IDL, not GDL, to use the C portions of 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 #89091 is a reply to message #89083] Wed, 23 July 2014 16:42 Go to previous messageGo to next message
sweiss1993 is currently offline  sweiss1993
Messages: 4
Registered: July 2014
Junior Member
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 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.

Thanks,
Sam
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 Go to previous message
Michael Galloy is currently offline  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
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: rebin vs congrid
Next Topic: IDL and C inter-language communication / call_external()

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 11:33:56 PDT 2025

Total time taken to generate the page: 0.00553 seconds