2 static char sfs_c_rndSid[] = "@(#)sfs_c_rnd.c 2.1 97/10/23";
6 * Copyright (c) 1992-1997,2001 by Standard Performance Evaluation Corporation
8 * Standard Performance Evaluation Corporation (SPEC)
9 * 6585 Merchant Place, Suite 100
12 * This product contains benchmarks acquired from several sources who
13 * understand and agree with SPEC's goal of creating fair and objective
14 * benchmarks to measure computer performance.
16 * This copyright notice is placed here only to protect SPEC in the
17 * event the source is misused in any manner that is contrary to the
18 * spirit, the goals and the intent of SPEC.
20 * The source code is provided to the user or company under the license
21 * agreement for the SPEC Benchmark Suite for this product.
24 /*****************************************************************
26 * Copyright 1991,1992 Legato Systems, Inc. *
27 * Copyright 1991,1992 Auspex Systems, Inc. *
28 * Copyright 1991,1992 Data General Corporation *
29 * Copyright 1991,1992 Digital Equipment Corporation *
30 * Copyright 1991,1992 Interphase Corporation *
31 * Copyright 1991,1992 Sun Microsystems, Inc. *
33 *****************************************************************/
36 * ---------------------- sfs_c_rnd.c ---------------------
38 * Random number generator.
41 * int32_t sfs_random(void)
42 * void sfs_srandom(int)
46 * int32_t spec_rand(void)
47 * void spec_srand(int)
50 * 28-Nov-91 Teelucksingh ANSI C
51 * 01-Aug-91 Wiryaman sfs_srandom() and sfs_random()
52 * now use spec_srand() and spec_rand()
53 * instead of srandom() and random().
54 * 17-Apr-91 Wittle Created.
67 #include <sys/types.h>
72 #include "sfs_c_def.h"
73 #include "sfs_m_def.h"
76 * Here's the source for the random number generator that SPEC uses.
77 * The function to be called is "spec_rand" which returns an integer
78 * between 1 and MAX_INT-1.
80 * One question we may wanna think about is the seeding of the random
81 * number generator. Do we start with the same seed everytime (for
82 * repeatability) or use a array of possible seeds (some seeds are better
83 * than others and SPEC prople mention that they have a list of 15
89 * ------------------------- Static Declarations -------------------------
92 static int32_t seedi = 2231;
96 * ------------------------- External Definitions -------------------------
99 static double ran(void);
100 static int32_t spec_rand(void);
101 static void spec_srand(int);
104 * ----------------------- Random Number Routines -----------------------
109 * Seed the random number generator.
120 * Returns a random number.
131 * Compute the next random number.
136 /* See "Random Number Generators: Good Ones Are Hard To Find", */
137 /* Park & Miller, CACM 31#10 October 1988 pages 1192-1201. */
138 /***********************************************************/
139 /* THIS IMPLEMENTATION REQUIRES AT LEAST 32 BIT INTEGERS ! */
140 /***********************************************************/
142 #define _A_MULTIPLIER 16807L
143 #define _M_MODULUS 2147483647L /* (2**31)-1 */
144 #define _Q_QUOTIENT 127773L /* 2147483647 / 16807 */
145 #define _R_REMAINDER 2836L /* 2147483647 % 16807 */
151 hi = seedi / _Q_QUOTIENT;
152 lo = seedi % _Q_QUOTIENT;
153 test = _A_MULTIPLIER * lo - _R_REMAINDER * hi;
157 seedi = test + _M_MODULUS;
159 return((float) seedi / _M_MODULUS);
163 * Local interface to seed random number generator.
174 * Local interface to obtain a random number.
182 static struct r_array {
187 static int r_length = 0;
190 r_array_compare(const void *i, const void *j)
192 if (((struct r_array *)i)->n2 > ((struct r_array *)j)->n2)
194 if (((struct r_array *)i)->n2 < ((struct r_array *)j)->n2)
200 init_rand_range(int length)
205 * If array already exists free it
213 * If length is zero just free memory and return
219 * Allocate array of sequential numbers and random numbers
221 if ((r_array = malloc(length * sizeof(struct r_array))) == NULL)
227 * Initialize array of sequential values and random values
229 for (i = 0; i < length; i++) {
231 r_array[i].n2 = sfs_random();
235 * Sort random array values to put sequential values in random order
237 qsort(r_array, length, sizeof(struct r_array), r_array_compare);
243 rand_range(int index)
245 return (r_array[index].n1);