src/util/random.c File Reference

#include "util.h"
Include dependency graph for random.c:

Go to the source code of this file.

Defines

#define MODULUS1   2147483563
#define LEQA1   40014
#define LEQQ1   53668
#define LEQR1   12211
#define MODULUS2   2147483399
#define LEQA2   40692
#define LEQQ2   52774
#define LEQR2   3791
#define STAB_SIZE   64
#define STAB_DIV   (1 + (MODULUS1 - 1) / STAB_SIZE)

Functions

void util_srandom (long seed)
long util_random (void)

Variables

static char rcsid[] UNUSED = "$Id: random.c,v 1.4 2002/09/10 00:05:33 fabio Exp $"
static long utilRand = 0
static long utilRand2
static long shuffleSelect
static long shuffleTable [STAB_SIZE]

Define Documentation

#define LEQA1   40014

Definition at line 42 of file random.c.

#define LEQA2   40692

Definition at line 46 of file random.c.

#define LEQQ1   53668

Definition at line 43 of file random.c.

#define LEQQ2   52774

Definition at line 47 of file random.c.

#define LEQR1   12211

Definition at line 44 of file random.c.

#define LEQR2   3791

Definition at line 48 of file random.c.

#define MODULUS1   2147483563

Definition at line 41 of file random.c.

#define MODULUS2   2147483399

Definition at line 45 of file random.c.

#define STAB_DIV   (1 + (MODULUS1 - 1) / STAB_SIZE)

Definition at line 50 of file random.c.

#define STAB_SIZE   64

Definition at line 49 of file random.c.


Function Documentation

long util_random ( void   ) 

Function********************************************************************

Synopsis [Portable random number generator.]

Description [Portable number generator based on ran2 from "Numerical Recipes in C." It is a long period (> 2 * 10^18) random number generator of L'Ecuyer with Bays-Durham shuffle. Returns a long integer uniformly distributed between 0 and 2147483561 (inclusive of the endpoint values). The random generator can be explicitly initialized by calling util_srandom. If no explicit initialization is performed, then the seed 1 is assumed.]

SideEffects []

SeeAlso [util_srandom]

Definition at line 143 of file random.c.

00144 {
00145     int i;      /* index in the shuffle table */
00146     long int w; /* work variable */
00147 
00148     /* utilRand == 0 if the geneartor has not been initialized yet. */
00149     if (utilRand == 0) util_srandom(1);
00150 
00151     /* Compute utilRand = (utilRand * LEQA1) % MODULUS1 avoiding
00152     ** overflows by Schrage's method.
00153     */
00154     w          = utilRand / LEQQ1;
00155     utilRand   = LEQA1 * (utilRand - w * LEQQ1) - w * LEQR1;
00156     utilRand  += (utilRand < 0) * MODULUS1;
00157 
00158     /* Compute utilRand2 = (utilRand2 * LEQA2) % MODULUS2 avoiding
00159     ** overflows by Schrage's method.
00160     */
00161     w          = utilRand2 / LEQQ2;
00162     utilRand2  = LEQA2 * (utilRand2 - w * LEQQ2) - w * LEQR2;
00163     utilRand2 += (utilRand2 < 0) * MODULUS2;
00164 
00165     /* utilRand is shuffled with the Bays-Durham algorithm.
00166     ** shuffleSelect and utilRand2 are combined to generate the output.
00167     */
00168 
00169     /* Pick one element from the shuffle table; "i" will be in the range
00170     ** from 0 to STAB_SIZE-1.
00171     */
00172     i = shuffleSelect / STAB_DIV;
00173     /* Mix the element of the shuffle table with the current iterate of
00174     ** the second sub-generator, and replace the chosen element of the
00175     ** shuffle table with the current iterate of the first sub-generator.
00176     */
00177     shuffleSelect   = shuffleTable[i] - utilRand2;
00178     shuffleTable[i] = utilRand;
00179     shuffleSelect  += (shuffleSelect < 1) * (MODULUS1 - 1);
00180     /* Since shuffleSelect != 0, and we want to be able to return 0,
00181     ** here we subtract 1 before returning.
00182     */
00183     return(shuffleSelect - 1);
00184 
00185 } /* end of util_random */

void util_srandom ( long  seed  ) 

AutomaticStart AutomaticEnd Function********************************************************************

Synopsis [Initializer for the portable random number generator.]

Description [Initializer for the portable number generator based on ran2 in "Numerical Recipes in C." The input is the seed for the generator. If it is negative, its absolute value is taken as seed. If it is 0, then 1 is taken as seed. The initialized sets up the two recurrences used to generate a long-period stream, and sets up the shuffle table.]

SideEffects [None]

SeeAlso [util_random]

Definition at line 106 of file random.c.

00107 {
00108     int i;
00109 
00110     if (seed < 0)       utilRand = -seed;
00111     else if (seed == 0) utilRand = 1;
00112     else                utilRand = seed;
00113     utilRand2 = utilRand;
00114     /* Load the shuffle table (after 11 warm-ups). */
00115     for (i = 0; i < STAB_SIZE + 11; i++) {
00116         long int w;
00117         w = utilRand / LEQQ1;
00118         utilRand = LEQA1 * (utilRand - w * LEQQ1) - w * LEQR1;
00119         utilRand += (utilRand < 0) * MODULUS1;
00120         shuffleTable[i % STAB_SIZE] = utilRand;
00121     }
00122     shuffleSelect = shuffleTable[1 % STAB_SIZE];
00123 } /* end of util_srandom */


Variable Documentation

long shuffleSelect [static]

Definition at line 67 of file random.c.

long shuffleTable[STAB_SIZE] [static]

Definition at line 68 of file random.c.

char rcsid [] UNUSED = "$Id: random.c,v 1.4 2002/09/10 00:05:33 fabio Exp $" [static]

CFile***********************************************************************

FileName [random.c]

PackageName [util]

Synopsis [Our own portable random number generator.]

Author [Fabio Somenzi <Fabio@Colorado.EDU>]

Copyright [Copyright (c) 1994-1996 The Regents of the Univ. of California. All rights reserved.

Permission is hereby granted, without written agreement and without license or royalty fees, to use, copy, modify, and distribute this software and its documentation for any purpose, provided that the above copyright notice and the following two paragraphs appear in all copies of this software.

IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN \"AS IS\" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.]

Definition at line 34 of file random.c.

long utilRand = 0 [static]

Definition at line 65 of file random.c.

long utilRand2 [static]

Definition at line 66 of file random.c.


Generated on Tue Jan 12 13:57:29 2010 for glu-2.2 by  doxygen 1.6.1