Reading and writing GSL random number generators
Olaf Lenz
olenz@Physik.Uni-Bielefeld.DE
Wed May 28 14:27:00 GMT 2003
Hello!
I'm using the GSL since about two years now, and I think that it's
time to contribute, even though it is hardly more than some technical
aspect.
I've noticed that a few people in the past have asked for functions to
read and write the state of a random number generator to a
file/stream. As I will need it myself, I've written these functions.
I've read the GSL Design Document, I know the Coding Standards, and
I've tried to stay closely to the code Brian has created for
combinations and/or permutations, so it should integrate smoothly.
If the code is acceptable, I will also add the corresponding
documentation.
In the attachments, you'll find:
- a patch for the gsl sources
- rng/file.c: the (new) file that contains the io routines
- rng_write.c: a test program that writes the state of an rng
- rng_read.c: a test program that reads the state of an rng
--
Olaf
-------------- next part --------------
Index: rng/gsl_rng.h
===================================================================
RCS file: /cvs/gsl/gsl/rng/gsl_rng.h,v
retrieving revision 1.51
diff -u -r1.51 gsl_rng.h
--- rng/gsl_rng.h 14 Jun 2002 21:13:47 -0000 1.51
+++ rng/gsl_rng.h 28 May 2003 14:00:48 -0000
@@ -135,6 +135,12 @@
unsigned long int gsl_rng_max (const gsl_rng * r);
unsigned long int gsl_rng_min (const gsl_rng * r);
const char *gsl_rng_name (const gsl_rng * r);
+
+int gsl_rng_fread (FILE * stream, gsl_rng * r);
+int gsl_rng_fwrite (FILE * stream, const gsl_rng * r);
+int gsl_rng_fprintf (FILE * stream, const gsl_rng * r, const char * format);
+int gsl_rng_fscanf (FILE * stream, gsl_rng * r);
+
size_t gsl_rng_size (const gsl_rng * r);
void * gsl_rng_state (const gsl_rng * r);
Index: rng/Makefile.am
===================================================================
RCS file: /cvs/gsl/gsl/rng/Makefile.am,v
retrieving revision 1.40
diff -u -r1.40 Makefile.am
--- rng/Makefile.am 13 May 2002 20:35:32 -0000 1.40
+++ rng/Makefile.am 28 May 2003 14:00:52 -0000
@@ -4,7 +4,7 @@
INCLUDES= -I$(top_builddir)
-libgslrng_la_SOURCES = borosh13.c cmrg.c coveyou.c default.c fishman18.c fishman20.c fishman2x.c gfsr4.c knuthran2.c knuthran.c lecuyer21.c minstd.c mrg.c mt.c r250.c ran0.c ran1.c ran2.c ran3.c rand48.c rand.c random.c randu.c ranf.c ranlux.c ranlxd.c ranlxs.c ranmar.c rng.c slatec.c taus.c taus113.c transputer.c tt.c types.c uni32.c uni.c vax.c waterman14.c zuf.c
+libgslrng_la_SOURCES = borosh13.c cmrg.c coveyou.c default.c file.c fishman18.c fishman20.c fishman2x.c gfsr4.c knuthran2.c knuthran.c lecuyer21.c minstd.c mrg.c mt.c r250.c ran0.c ran1.c ran2.c ran3.c rand48.c rand.c random.c randu.c ranf.c ranlux.c ranlxd.c ranlxs.c ranmar.c rng.c slatec.c taus.c taus113.c transputer.c tt.c types.c uni32.c uni.c vax.c waterman14.c zuf.c
test_SOURCES = test.c
-------------- next part --------------
/* rng/file.c
*
* Copyright (C) 1996, 1997, 1998, 1999, 2000 Brian Gough
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <config.h>
#include <stdio.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_rng.h>
#define IN_FORMAT "%lx"
int
gsl_rng_fread (FILE * stream, gsl_rng * r)
{
size_t n = r->type->size ;
size_t * state = r->state;
size_t items = fread (state, sizeof (size_t), n, stream);
if (items != n)
{
GSL_ERROR ("fread failed", GSL_EFAILED);
}
return GSL_SUCCESS;
}
int
gsl_rng_fwrite (FILE * stream, const gsl_rng * r)
{
size_t n = r->type->size ;
size_t * state = r->state;
size_t items = fwrite (state, sizeof (size_t), n, stream);
if (items != n)
{
GSL_ERROR ("fwrite failed", GSL_EFAILED);
}
return GSL_SUCCESS;
}
int
gsl_rng_fprintf (FILE * stream, const gsl_rng * r, const char * format)
{
size_t n = r->type->size ;
size_t * state = r->state;
size_t i;
for (i = 0; i < n; i++)
{
int status = fprintf (stream, format, state[i]);
if (status < 0)
{
GSL_ERROR ("fprintf failed", GSL_EFAILED);
}
}
return GSL_SUCCESS;
}
int
gsl_rng_fscanf (FILE * stream, gsl_rng * r)
{
size_t n = r->type->size ;
size_t * state = r->state;
size_t i;
for (i = 0; i < n; i++)
{
unsigned long j ;
/* FIXME: what if size_t != unsigned long ???
want read in size_t but have to read in unsigned long to avoid
error from compiler */
int status = fscanf (stream, IN_FORMAT, &j);
if (status != 1)
{
GSL_ERROR ("fscanf failed", GSL_EFAILED);
}
state[i] = j;
}
return GSL_SUCCESS;
}
-------------- next part --------------
#include <stdio.h>
#include <gsl/gsl_rng.h>
#define N 1000
int main() {
int i;
unsigned long v;
FILE *file;
gsl_rng *r;
gsl_rng_env_setup();
r = gsl_rng_alloc(gsl_rng_default);
gsl_rng_set(r, 42);
for (i = 0; i < N; i++)
v = gsl_rng_get(r);
file=fopen("rng.bin", "w");
gsl_rng_fwrite(file, r);
fclose(file);
file=fopen("rng.txt", "w");
gsl_rng_fprintf(file, r, "%lx ");
fclose(file);
for (i = 0; i < N; i++)
v = gsl_rng_get(r);
v = gsl_rng_get(r);
printf("Got %ul after generating %i random numbers.\n", v, N);
gsl_rng_free(r);
return 0;
}
-------------- next part --------------
#include <stdio.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_errno.h>
#define N 1000
int main() {
int i;
unsigned long v;
FILE *file1;
FILE *file2;
gsl_rng *r;
gsl_set_error_handler_off();
gsl_rng_env_setup();
r = gsl_rng_alloc(gsl_rng_default);
file1 = fopen("rng.bin", "r");
gsl_rng_fread(file1, r);
fclose(file1);
for (i=0; i < N; i++)
v = gsl_rng_get(r);
v = gsl_rng_get(r);
printf("Got %ul after generating %i random numbers.\n", v, N);
file2 = fopen("rng.txt", "r");
gsl_rng_fscanf(file2, r);
fclose(file2);
for (i=0; i < N; i++)
v = gsl_rng_get(r);
v = gsl_rng_get(r);
printf("Got %ul after generating %i random numbers.\n", v, N);
gsl_rng_free(r);
return 0;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 184 bytes
Desc: not available
URL: <http://sourceware.org/pipermail/gsl-discuss/attachments/20030528/25452a2e/attachment.sig>
More information about the Gsl-discuss
mailing list