Results 1 to 7 of 7
Hello all,
I am using GSL in c++ to generate a random sequence, and it's working fine. After generating many samples (say at point X), I need to keep the ...
- 10-29-2009 #1Just Joined!
- Join Date
- Oct 2009
- Posts
- 4
How to reproduce a GSL random sequence
Hello all,
I am using GSL in c++ to generate a random sequence, and it's working fine. After generating many samples (say at point X), I need to keep the state of the random generator in a file, to be able to reproduce the same sequence (from point X) later.
Is there any way for that?
I can write the state in a binary file as following:
void * state = gsl_rng_state (r);
size_t n = gsl_rng_size (r);
fwrite (state, n, 1, stream);
fclose (stream);
but next time I open it by following code, it doesn't generate the same numbers stated at point X.
stream = fopen ( "temp" , "rb" );
fread (state, n, 1, stream);
fclose (stream);
Any idea is appreciated.
Thanks,
naderi
- 10-29-2009 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Not knowing what algorithm and details of implementation GSL uses for random number generation, one could not say why this is. However, I think your assumption that the value returned by gsl_rng_size(r) is the same as the size of the data pointed to by the state variable is questionable.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 10-29-2009 #3Just Joined!
- Join Date
- Oct 2009
- Posts
- 4
OK, I am looking for a uniform random generator. and what is the true assumption?
My sample code to write the state of the generator is following.
////////////////////////////////
gsl_rng * r; /* global generator */
int main (void)
{
const gsl_rng_type * T;
gsl_rng_env_setup();
T = gsl_rng_default;
r = gsl_rng_alloc (T);
gsl_rng_set ( r, 8880);
printf ("generator type: %s\n", gsl_rng_name (r));
gsl_rng_uniform_int(r, 100); /// some sample from the random generator
gsl_rng_uniform_int(r, 100);
gsl_rng_uniform_int(r, 100);
FILE *stream;
stream = fopen ( "temp" , "wb" ); /// opennig a file to write the state
void * state = gsl_rng_state (r);
size_t n = gsl_rng_size (r);
fwrite (state, n, 1, stream);
fclose (stream);
cout << gsl_rng_uniform_int(r, 100)<< " "<<gsl_rng_uniform_int(r, 100)<<"\n";
cout << gsl_rng_uniform_int(r, 100)<< " "<<gsl_rng_uniform_int(r, 100)<<"\n";
gsl_rng_free (r);
return 0;
}
Results:
//////////////////////////
generator type: mt19937
29 2
7 29
//////////////////////////
//Sample code to read back the state :
//////////////////////////
gsl_rng * r; /* global generator */
int main (void)
{
const gsl_rng_type * T;
gsl_rng_env_setup();
T = gsl_rng_default;
r = gsl_rng_alloc (T);
gsl_rng_set ( r, 8880); /// could have a diferent seed
printf ("generator type: %s\n", gsl_rng_name (r));
gsl_rng_uniform_int(r, 100); /// some sample from the random generator
gsl_rng_uniform_int(r, 100);
FILE *stream;
void * state;
size_t n;
stream = fopen ( "temp" , "rb" );
fread (state, n, 1, stream);
fclose (stream);
cout << gsl_rng_uniform_int(r, 100)<< " "<<gsl_rng_uniform_int(r, 100)<<"\n";
cout << gsl_rng_uniform_int(r, 100)<< " "<<gsl_rng_uniform_int(r, 100)<<"\n";
gsl_rng_free (r);
return 0;
}
Results:
//////////////////////////
generator type: mt19937
2 61
29 29
//////////////////////////
As you see the results are not match.
- 10-29-2009 #4Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
You didn't initialize the state variable in the program to read the old state. In fact, I'm surprised you didn't get a core dump here.
Code:. . . FILE *stream; void * state; size_t n; stream = fopen ( "temp" , "rb" ); fread (state, n, 1, stream); fclose (stream); . . .
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 10-29-2009 #5Just Joined!
- Join Date
- Oct 2009
- Posts
- 4
Thanks, but how is the initializtion? what command can initialize the generator for the state?
naderi
- 10-29-2009 #6Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
I would think you need to do most of the setup that you did in the process that created the rng in the first place. Finally, you can get the state and then the size to read from the file you stored it in originally.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 10-30-2009 #7Just Joined!
- Join Date
- Oct 2009
- Posts
- 4
I got the solution. The following commands can do the job:
int gsl_rng_fwrite (FILE * stream, const gsl_rng * r)
int gsl_rng_fread (FILE * stream, gsl_rng * r)
naderi


Reply With Quote