Main Page | Class List | Directories | File List | Class Members | File Members

random.c File Reference

#include "random.h"
#include <stdio.h>

Go to the source code of this file.

Defines

#define TYPE_0   0
#define BREAK_0   8
#define DEG_0   0
#define SEP_0   0
#define TYPE_1   1
#define BREAK_1   32
#define DEG_1   7
#define SEP_1   3
#define TYPE_2   2
#define BREAK_2   64
#define DEG_2   15
#define SEP_2   1
#define TYPE_3   3
#define BREAK_3   128
#define DEG_3   31
#define SEP_3   3
#define TYPE_4   4
#define BREAK_4   256
#define DEG_4   63
#define SEP_4   1
#define MAX_TYPES   5

Functions

static long good_rand __P ((long))
static long good_rand (long x)
void srandom (unsigned long x)
char * initstate (unsigned long seed, char *arg_state, long n)
char * setstate (char *arg_state)
long random ()

Variables

static long degrees [MAX_TYPES] = { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 }
static long seps [MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 }
static long randtbl [DEG_3+1]
static long * fptr = &randtbl[SEP_3 + 1]
static long * rptr = &randtbl[1]
static long * state = &randtbl[1]
static long rand_type = TYPE_3
static long rand_deg = DEG_3
static long rand_sep = SEP_3
static long * end_ptr = &randtbl[DEG_3 + 1]


Define Documentation

#define BREAK_0   8
 

Definition at line 140 of file random.c.

Referenced by initstate().

#define BREAK_1   32
 

Definition at line 145 of file random.c.

Referenced by initstate().

#define BREAK_2   64
 

Definition at line 150 of file random.c.

Referenced by initstate().

#define BREAK_3   128
 

Definition at line 155 of file random.c.

Referenced by initstate().

#define BREAK_4   256
 

Definition at line 160 of file random.c.

Referenced by initstate().

#define DEG_0   0
 

Definition at line 141 of file random.c.

Referenced by initstate().

#define DEG_1   7
 

Definition at line 146 of file random.c.

Referenced by initstate().

#define DEG_2   15
 

Definition at line 151 of file random.c.

Referenced by initstate().

#define DEG_3   31
 

Definition at line 156 of file random.c.

Referenced by initstate().

#define DEG_4   63
 

Definition at line 161 of file random.c.

Referenced by initstate().

#define MAX_TYPES   5
 

Definition at line 168 of file random.c.

Referenced by initstate(), and setstate().

#define SEP_0   0
 

Definition at line 142 of file random.c.

Referenced by initstate().

#define SEP_1   3
 

Definition at line 147 of file random.c.

Referenced by initstate().

#define SEP_2   1
 

Definition at line 152 of file random.c.

Referenced by initstate().

#define SEP_3   3
 

Definition at line 157 of file random.c.

Referenced by initstate().

#define SEP_4   1
 

Definition at line 162 of file random.c.

Referenced by initstate().

#define TYPE_0   0
 

Definition at line 139 of file random.c.

Referenced by initstate(), random(), setstate(), and srandom().

#define TYPE_1   1
 

Definition at line 144 of file random.c.

Referenced by initstate(), and setstate().

#define TYPE_2   2
 

Definition at line 149 of file random.c.

Referenced by initstate(), and setstate().

#define TYPE_3   3
 

Definition at line 154 of file random.c.

Referenced by initstate(), and setstate().

#define TYPE_4   4
 

Definition at line 159 of file random.c.

Referenced by initstate(), and setstate().


Function Documentation

static long good_rand __P (long)   )  [static]
 

static long good_rand long  x  )  [static]
 

Definition at line 243 of file random.c.

Referenced by random(), and srandom().

00245 {
00246 #ifdef  USE_WEAK_SEEDING
00247 /*
00248  * Historic implementation compatibility.
00249  * The random sequences do not vary much with the seed,
00250  * even with overflowing.
00251  */
00252         return (1103515245 * x + 12345);
00253 #else   /* !USE_WEAK_SEEDING */
00254 /*
00255  * Compute x = (7^5 * x) mod (2^31 - 1)
00256  * wihout overflowing 31 bits:
00257  *      (2^31 - 1) = 127773 * (7^5) + 2836
00258  * From "Random number generators: good ones are hard to find",
00259  * Park and Miller, Communications of the ACM, vol. 31, no. 10,
00260  * October 1988, p. 1195.
00261  */
00262         register long hi, lo;
00263 
00264         hi = x / 127773;
00265         lo = x % 127773;
00266         x = 16807 * lo - 2836 * hi;
00267         if (x <= 0)
00268                 x += 0x7fffffff;
00269         return (x);
00270 #endif  /* !USE_WEAK_SEEDING */
00271 }

char* initstate unsigned long  seed,
char *  arg_state,
long  n
 

Definition at line 388 of file random.c.

References BREAK_0, BREAK_1, BREAK_2, BREAK_3, BREAK_4, DEG_0, DEG_1, DEG_2, DEG_3, DEG_4, end_ptr, MAX_TYPES, rand_deg, rand_sep, rand_type, rptr, SEP_0, SEP_1, SEP_2, SEP_3, SEP_4, srandom, TYPE_0, TYPE_1, TYPE_2, TYPE_3, and TYPE_4.

00392 {
00393         register char *ostate = (char *)(&state[-1]);
00394         register long *long_arg_state = (long *) arg_state;
00395 
00396         if (rand_type == TYPE_0)
00397                 state[-1] = rand_type;
00398         else
00399                 state[-1] = MAX_TYPES * (rptr - state) + rand_type;
00400         if (n < BREAK_0) {
00401                 (void)fprintf(stderr,
00402                     "random: not enough state (%ld bytes); ignored.\n", n);
00403                 return(0);
00404         }
00405         if (n < BREAK_1) {
00406                 rand_type = TYPE_0;
00407                 rand_deg = DEG_0;
00408                 rand_sep = SEP_0;
00409         } else if (n < BREAK_2) {
00410                 rand_type = TYPE_1;
00411                 rand_deg = DEG_1;
00412                 rand_sep = SEP_1;
00413         } else if (n < BREAK_3) {
00414                 rand_type = TYPE_2;
00415                 rand_deg = DEG_2;
00416                 rand_sep = SEP_2;
00417         } else if (n < BREAK_4) {
00418                 rand_type = TYPE_3;
00419                 rand_deg = DEG_3;
00420                 rand_sep = SEP_3;
00421         } else {
00422                 rand_type = TYPE_4;
00423                 rand_deg = DEG_4;
00424                 rand_sep = SEP_4;
00425         }
00426         state = (long *) (long_arg_state + 1); /* first location */
00427         end_ptr = &state[rand_deg];     /* must set end_ptr before srandom */
00428         srandom(seed);
00429         if (rand_type == TYPE_0)
00430                 long_arg_state[0] = rand_type;
00431         else
00432                 long_arg_state[0] = MAX_TYPES * (rptr - state) + rand_type;
00433         return(ostate);
00434 }

long random  ) 
 

Definition at line 509 of file random.c.

References end_ptr, fptr, good_rand(), i, rand_type, rptr, and TYPE_0.

00510 {
00511         register long i;
00512         register long *f, *r;
00513 
00514         if (rand_type == TYPE_0) {
00515                 i = state[0];
00516                 state[0] = i = (good_rand(i)) & 0x7fffffff;
00517         } else {
00518                 /*
00519                  * Use local variables rather than static variables for speed.
00520                  */
00521                 f = fptr; r = rptr;
00522                 *f += *r;
00523                 i = (*f >> 1) & 0x7fffffff;     /* chucking least random bit */
00524                 if (++f >= end_ptr) {
00525                         f = state;
00526                         ++r;
00527                 }
00528                 else if (++r >= end_ptr) {
00529                         r = state;
00530                 }
00531 
00532                 fptr = f; rptr = r;
00533         }
00534         return(i);
00535 }

char* setstate char *  arg_state  ) 
 

Definition at line 456 of file random.c.

References degrees, end_ptr, fptr, MAX_TYPES, rand_deg, rand_sep, rand_type, rptr, seps, TYPE_0, TYPE_1, TYPE_2, TYPE_3, and TYPE_4.

00458 {
00459         register long *new_state = (long *) arg_state;
00460         register long type = new_state[0] % MAX_TYPES;
00461         register long rear = new_state[0] / MAX_TYPES;
00462         char *ostate = (char *)(&state[-1]);
00463 
00464         if (rand_type == TYPE_0)
00465                 state[-1] = rand_type;
00466         else
00467                 state[-1] = MAX_TYPES * (rptr - state) + rand_type;
00468         switch(type) {
00469         case TYPE_0:
00470         case TYPE_1:
00471         case TYPE_2:
00472         case TYPE_3:
00473         case TYPE_4:
00474                 rand_type = type;
00475                 rand_deg = degrees[type];
00476                 rand_sep = seps[type];
00477                 break;
00478         default:
00479                 (void)fprintf(stderr,
00480                     "random: state info corrupted; not changed.\n");
00481         }
00482         state = (long *) (new_state + 1);
00483         if (rand_type != TYPE_0) {
00484                 rptr = &state[rear];
00485                 fptr = &state[(rear + rand_sep) % rand_deg];
00486         }
00487         end_ptr = &state[rand_deg];             /* set end_ptr too */
00488         return(ostate);
00489 }

void srandom unsigned long  x  ) 
 

Definition at line 286 of file random.c.

References fptr, good_rand(), i, rand_deg, rand_sep, rand_type, random, rptr, state, and TYPE_0.

00288 {
00289         register long i;
00290 
00291         if (rand_type == TYPE_0)
00292                 state[0] = x;
00293         else {
00294                 state[0] = x;
00295                 for (i = 1; i < rand_deg; i++)
00296                         state[i] = good_rand(state[i - 1]);
00297                 fptr = &state[rand_sep];
00298                 rptr = &state[0];
00299                 for (i = 0; i < 10 * rand_deg; i++)
00300                         (void)random();
00301         }
00302 }


Variable Documentation

long degrees[MAX_TYPES] = { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 } [static]
 

Definition at line 170 of file random.c.

Referenced by setstate().

long* end_ptr = &randtbl[DEG_3 + 1] [static]
 

Definition at line 239 of file random.c.

Referenced by initstate(), random(), and setstate().

long* fptr = &randtbl[SEP_3 + 1] [static]
 

Definition at line 222 of file random.c.

Referenced by random(), setstate(), and srandom().

long rand_deg = DEG_3 [static]
 

Definition at line 237 of file random.c.

Referenced by initstate(), setstate(), and srandom().

long rand_sep = SEP_3 [static]
 

Definition at line 238 of file random.c.

Referenced by initstate(), setstate(), and srandom().

long rand_type = TYPE_3 [static]
 

Definition at line 236 of file random.c.

Referenced by initstate(), random(), setstate(), and srandom().

long randtbl[DEG_3+1] [static]
 

Initial value:

 {
        TYPE_3,









   
        0x991539b1, 0x16a5bce3, 0x6774a4cd, 0x3e01511e, 0x4e508aaa, 0x61048c05,
        0xf5500617, 0x846b7115, 0x6a19892c, 0x896a97af, 0xdb48f936, 0x14898454,
        0x37ffd106, 0xb58bff9c, 0x59e17104, 0xcf918a49, 0x09378c83, 0x52c7a471,
        0x8d293ea9, 0x1f4fc301, 0xc3db71be, 0x39b44e1c, 0xf8a44ef9, 0x4c8b80b1,
        0x19edc328, 0x87bf4bdd, 0xc9b240e5, 0xe9ee4b1b, 0x4382aee7, 0x535b6b41,
        0xf3bec5da

}

Definition at line 187 of file random.c.

long* rptr = &randtbl[1] [static]
 

Definition at line 223 of file random.c.

Referenced by initstate(), random(), setstate(), and srandom().

long seps[MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 } [static]
 

Definition at line 171 of file random.c.

Referenced by setstate().

long* state = &randtbl[1] [static]
 

Definition at line 235 of file random.c.


© sourcejam.com 2005-2008