[Malvern] Fw: Random Number Generator

Phil Ironside phil at creativespaces.co.uk
Fri Sep 22 15:38:40 BST 2006


Hi Dean

You just address a mail to malvern at mailman.lug.org.uk with the appropriate Subject and off you go. Just remember that the contents of any mails are kept in an archive which is publically available.

I've forwarded this one to the list.

Begin forwarded message:

Date: Fri, 22 Sep 2006 14:07:44 +0100
From: m g william <mgbg25171 at blueyonder.co.uk>
To: phil at cstf.co.uk
Subject: Request from Malvern LUG


Hi Phil
I've subscribed to the mailing list but am not sure how I post to it so
here's that stuff for Chris.

I'm trying to work out the 'skeleton' that any random number generator
must conform to, in order for it to be compatible with (ie capable of
'powering') the distributions in gsl_randist.h.

I'm making progress and have attached 3 files re this stuff.
gsl_rng_alloc shows the data structures and setup of an instance. I want
to use c++ objects to make my rng so I'll need to map it onto the c
stuff in gsl. 

rgbrown is some bloke in a US university who seems to have a grasp of
this sort of thing.

rand3 just shows how to use a gsl rng to 'power' a couple of
distributions. 

The other night was well worth the drive, so see you soon.
Dean




-- 
Phil Ironside

Creative Spaces
01684-561495

-------------- next part --------------


=============================================================================
Member Data-types
A datatype called size_t is defined in the stdlib.h library, 
which represents sizes of an array in the member functions of the library. 
In practice, size_t is often assumed to have the same storage requirements as an unsigned integer. 
Because the actual size of size_t is architecture-dependent, 
this assumption can lead to programming errors, 
particularly as 64-bit architectures become more prevalent.
=============================================================================


=============================================================================
			gsl_rng_type 
-dean specifies the static data about a type of generator

typedef struct
{
	//these elements are effectively Public Attributes
	const char *name;
	unsigned long int max;
	unsigned long int min;
	size_t size;
	void (*set) (void *state, unsigned long int seed);
	unsigned long int (*get) (void *state);
	double (*get_double) (void *state);
}
gsl_rng_type;

This is the complete list of member methods for gsl_rng_type, including all inherited members.
get		
get_double	
max	
min	
name	
set	
size	

=============================================================================
			gsl_rng 
- dean specifies info about a particular instance of a type of generator

typedef struct
{
	//these elements are effectively Public Attributes
	const gsl_rng_type * type;
	void *state;
}
gsl_rng;

//no methods associated with this
=============================================================================
//taken from rng.cc (line 517)
			gsl_rng_alloc (const gsl_rng_type * T)
{
	gsl_rng *r = (gsl_rng *) malloc (sizeof (gsl_rng));
	if (r == 0) 
	{ 
		GSL_ERROR_VAL ("failed to allocate space for rng struct", GSL_ENOMEM, 0); 
	};
	r->state = malloc (T->size);

	if (r->state == 0)
	{
		free (r);         /* exception in constructor, avoid memory leak */
		GSL_ERROR_VAL ("failed to allocate space for rng state", GSL_ENOMEM, 0);
	};
	r->type = T;
	gsl_rng_set (r, gsl_rng_default_seed);        /* seed the generator */
	return r;
}

-------------- next part --------------
This is the mail archive of the gsl-discuss at sources.redhat.com mailing list for the GSL project.
Index Nav: 	[Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: 	[Date Prev] [Date Next] 	[Thread Prev] [Thread Next]
Other format: 	[Raw text]
Re: RN generators Seed

    * From: "Robert G. Brown" <rgb at phy dot duke dot edu>
    * To: Maura Edelweiss Monville <memonvil at artsci dot wustl dot edu>
    * Cc: GSL Discussion list <gsl-discuss at sources dot redhat dot com>
    * Date: Wed, 27 Aug 2003 14:58:08 -0400 (EDT)
    * Subject: Re: RN generators Seed

On Tue, 26 Aug 2003, Maura Edelweiss Monville wrote:

> Thank you very much. 
> Since I 'm using a RN generator from GSL I'm definitely interested in
> the gsl-compatible call wrapper for /dev/random. 
> I guess it is not in the same web site as the othe GSL goodies ... 
> I would appreciate being given the possibility to download it. 
> I develop on SuSE 8.2.

I'm including BOTH the code to generate a good random number seed from
/dev/random as a direct subroutine call (at the very end of this message) 
AND the wrapper code below.  The gsl rng god (if listening) is welcome to 
add /dev/random  to the gsl rng list of supported generators if they so desire.

[If you do you'll probably want to make error handling gsl-consistent (I'm
sloppy) and perhaps insert a stat/test for /dev/random's existence at the
point where the generator is actually added to the list of generators, as it
may not exist on some architectures where gsl runs.  You also MAY want to add
a gsl_rng_set_dev_random(random) function that just sets the seed from
/dev/random -- or not.]



To use the wrapper in your gsl routine, save dev_random.c below, add it to your 
makefile so it will compile and link.  Add the following into your header
file(s) (ordinarily they'd be in <gsl/gsl_rng.h>).

 const gsl_rng_type **types;    /* where all the rng types go */
 gsl_rng *random;               /* global gsl random number generator */

You'll then need a fragment like the following in your startup code to add the new type to the gsl
list of rng's BEFORE trying to use the new generator in any way:

%< Snip snip snip ==================================================================
 /*
  * Now add new RNG's onto the GSL types list.  Apparently this is all that is
  * needed -- they subsequently just "work" in the gsl call format.  This is code
  * taken from gsl types.c where N is apparently a hard-coded value.  This means
  * that we cannot CURRENTLY go over 100 generators total without e.g.
  * increasing N in the gsl sources.
  */
#define N 100
#define GSL_VAR

/* List new rng types to be added. */
 GSL_VAR const gsl_rng_type *gsl_rng_dev_random;

 int i,devrandom;

 /*
  * get to the end of the list of built in gsl generators
  */
 types = gsl_rng_types_setup ();
 i = 0;
 while(types[i] != NULL){
   i++;
   /* insert list code if desired */
 }
 /*
  * and add the new one(s)...
  */
 if(i < N){
   i = devrandom; /* save the index/type of the new generator */
   types[i++] = (gsl_rng_dev_random);
 }

%< Snip snip snip ==================================================================

Thereafter you should be able to use /dev/random just like any other gsl rng,
e.g.
       gsl_rng *random;
       random = gsl_rng_alloc (types[randnum]);
       random_max = gsl_rng_max(random);
       for(i = 0;i<100;i++){
         printf("%s returns a random int = %d\n",
           gsl_rng_name(random),    /* should return string "/dev/random", */
           gsl_rng_get(random));     /* should return a random int from /dev/random */
       }

(or the like, I'm not testing this code for typos:-) should return 100 random
ints from /dev/random.  If you run long enough loops of this, you can actually
see /dev/random "hang" from time to time waiting for entropy...

Really this is a pretty cool interface and it's remarkably easy to add your own rng's 
to it.

All code in this note is GPL v2, BTW and can be freely added to the GSL or
anybody's personal code, just to complete the formalities there.

   rgb

-- 
Robert G. Brown	                       http://www.phy.duke.edu/~rgb/
Duke University Dept. of Physics, Box 90305
Durham, N.C. 27708-0305
Phone: 1-919-660-2567  Fax: 919-660-2525     email:rgb at phy.duke.edu


%< Snip snip snip =============== dev_random.c ================================
/* dev_random
 * 
 * Copyright (C) 2003 Robert G. Brown
 * 
 * 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 <gsl/gsl_rng.h>

/*
 * This is a wrapping of the /dev/random hardware rng
 */

static unsigned long int dev_random_get (void *vstate);
static double dev_random_get_double (void *vstate);
static void dev_random_set (void *vstate, unsigned long int s);

typedef struct
  {
    FILE *fp;
  }
dev_random_state_t;

static unsigned long int
dev_random_get (void *vstate)
{
  dev_random_state_t *state = (dev_random_state_t *) vstate;
  unsigned long int j;

  if(state->fp != NULL) {
    fread(&j,sizeof(j),1,state->fp);
    return j;
  } else {
    fprintf(stderr,"Error: /dev/random not open.  Exiting.\n");
    exit(0);
  }

}

static double
dev_random_get_double (void *vstate)
{
  return dev_random_get (vstate) / (double) UINT_MAX;
}

static void
dev_random_set (void *vstate, unsigned long int s)
{
  dev_random_state_t *state = (dev_random_state_t *) vstate;

 if ((state->fp = fopen("/dev/random","r")) == NULL) {
   fprintf(stderr,"Error: Cannot open /dev/random, exiting.\n");
   exit(0);
 }

 return;

}

static const gsl_rng_type dev_random_type =
{"/dev/random",			/* name */
 UINT_MAX,			/* RAND_MAX */
 0,				/* RAND_MIN */
 sizeof (dev_random_state_t),
 &dev_random_set,
 &dev_random_get,
 &dev_random_get_double};

const gsl_rng_type *gsl_rng_dev_random = &dev_random_type;

%< Snip snip snip =============== end of dev_random.c   ============================

%< Snip snip snip =============== random_seed.c  ===================================
/*
 *========================================================================
 * $Id: random_seed.c,v 1.2 2003/06/10 15:21:15 rgb Exp $
 *
 * See copyright in copyright.h and the accompanying file COPYING
 *========================================================================
 */

/*
 * *========================================================================
 * NOTE WELL:  This routine automagically falls back on setting from clock if
 * /dev/random is not available.  It also is missing some includes -- presuming
 * it will be put somewhere with either a shared header (where e.g. verbose is
 * defined) that has them or you'll add them by hand so it can standalone...
 * *========================================================================
 */

unsigned long int random_seed()
{

 unsigned long int seed;
 struct timeval tv;
 FILE *devrandom;

 if ((devrandom = fopen("/dev/random","r")) == NULL) {
   gettimeofday(&tv,0);
   seed = tv.tv_sec + tv.tv_usec;
   if(verbose) printf("Got seed %u from gettimeofday()\n",seed);
 } else {
   fread(&seed,sizeof(seed),1,devrandom);
   if(verbose) printf("Got seed %u from /dev/random\n",seed);
   fclose(devrandom);
 }

 return(seed);

}

%< Snip snip snip ==================================================================



    * Follow-Ups:
          o Re: RN generators Seed
                + From: Robert G. Brown
          o EigenSystem GSL
                + From: Gaurav Bansal

Index Nav: 	[Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: 	[Date Prev] [Date Next] 	[Thread Prev] [Thread Next]

-------------- next part --------------
A non-text attachment was scrubbed...
Name: rand3.c
Type: text/x-csrc
Size: 380 bytes
Desc: not available
Url : http://mailman.lug.org.uk/pipermail/malvern/attachments/20060922/4a3ea896/rand3.bin


More information about the Malvern mailing list