Serialize random state/seed?

Jan Wielemaker J.Wielemaker at vu.nl
Thu Nov 10 14:15:54 CET 2011


Hi,

I'm using the GMP random functions in SWI-Prolog. I'm trying to emulate
the API of an old but de-facto standard random library for Prolog. One
of the operations there is to fetch the state of the random generator
and later restore it. The state is an arbitrary Prolog term, so you can
store it, or read/write it to a file.

I see that there is a function to clone a random state, but that doesn't
allow me to represent it as a Prolog term. In gmp.h, I find

typedef struct
{
   mpz_t _mp_seed;	  /* _mp_d member points to state of the generator. */
   gmp_randalg_t _mp_alg;  /* Currently unused. */
   union {
     void *_mp_lc;         /* Pointer to function pointers structure.  */
   } _mp_algdata;
} __gmp_randstate_struct;
typedef __gmp_randstate_struct gmp_randstate_t[1];

which half-way (the remark about _mp_d is suspicious) suggests to fetch
the _mp_seed member. Alas, when handled as a mpz_t, it is considered 0
because _mp_size is always 0.

I can set _mp_size to _mp_alloc in the seed to get a nice big number and
write a function to set this back using mpz_set on the _mp_seed member.

This all seems to work fine. I can keep track of the algorithm outside
GMP if needed. Just, it doesn't really feel like engineering (let alone
proper engineering) :-(

Am I right that this functionality is lacking?  If so, I'd like to ask
for a supported interface to serialize the state, for example as a single
GMP integer (after all, even if the state gets more complicated, anything
can be represented in a single big integer).

	Cheers --- Jan



More information about the gmp-discuss mailing list