1 ?RCS: $Id: randfunc.U,v 3.0 1993/08/18 12:09:39 ram Exp $
3 ?RCS: Copyright (c) 1991-1993, Raphael Manfredi
5 ?RCS: You may redistribute only under the terms of the Artistic Licence,
6 ?RCS: as specified in the README file that comes with the distribution.
7 ?RCS: You may reuse parts of this distribution only within the terms of
8 ?RCS: that same Artistic Licence; a copy of which may be found at the root
9 ?RCS: of the source tree for dist 3.0.
11 ?RCS: $Log: randfunc.U,v $
12 ?RCS: Revision 3.0 1993/08/18 12:09:39 ram
13 ?RCS: Baseline for dist 3.0 netwide release.
16 ?X: This is the new unit that should be used when random
17 ?X: functions are to be used. It thus makes randbits.U obsolete.
19 ?X: This unit gives a simple #define for Drand01, which produces
20 ?X: doubles in the range [0,1) using the "best" random number
21 ?X: generator available. The source should just call Drand01
22 ?X: and not worry about the underlying implementation.
23 ?X: Andy Dougherty July 1998
25 ?MAKE:randfunc drand01 seedfunc randbits randseedtype: \
26 cat rm_try test i_stdlib i_unistd Compile Myread Csym ccflags
27 ?MAKE: -pick add $@ %<
29 ?S: Indicates the name of the random number function to use.
30 ?S: Values include drand48, random, and rand. In C programs,
31 ?S: the 'Drand01' macro is defined to generate uniformly distributed
32 ?S: random numbers over the range [0., 1.[ (see drand01 and nrand).
35 ?S: Indicates the macro to be used to generate normalized
36 ?S: random numbers. Uses randfunc, often divided by
37 ?S: (double) (((unsigned long) 1 << randbits)) in order to
38 ?S: normalize the result.
39 ?S: In C programs, the macro 'Drand01' is mapped to drand01.
42 ?S: Indicates the type of the argument of the seedfunc.
45 ?S: Indicates the random number generating seed function.
46 ?S: Values include srand48, srandom, and srand.
49 ?S: Indicates how many bits are produced by the function used to
50 ?S: generate normalized random numbers.
53 ?C: This symbol defines the random function used to define Drand01().
56 ?C: This macro is to be used to generate uniformly distributed
57 ?C: random numbers over the range [0., 1.[. You may have to supply
58 ?C: an 'extern double drand48();' in your program since SunOS 4.1.3
59 ?C: doesn't provide you with anything relevant in its headers.
60 ?C: See HAS_DRAND48_PROTO.
63 ?C: This symbol defines the type of the argument of the
64 ?C: random seed function.
67 ?C: This symbol defines the macro to be used in seeding the
68 ?C: random number generator (see Drand01).
71 ?C: This symbol indicates how many bits are produced by the
72 ?C: function used to generate normalized random numbers.
73 ?C: Values include 15, 16, 31, and 48.
75 ?H:#define RANDFUNC $randfunc /**/
76 ?H:#define Drand01() $drand01 /**/
77 ?H:#define Rand_seed_t $randseedtype /**/
78 ?H:#define seedDrand01(x) $seedfunc((Rand_seed_t)x) /**/
79 ?H:#define RANDBITS $randbits /**/
83 : How can we generate normalized random numbers ?
85 echo "Looking for a random number function..." >&4
88 if set drand48 val -f; eval $csym; $val; then
90 echo "Good, found drand48()." >&4
91 elif set random val -f; eval $csym; $val; then
93 echo "OK, found random()." >&4
96 echo "Yick, looks like I have to use rand()." >&4
107 *-Dmy_rand=*|*-Dmy_srand=*)
108 echo "Removing obsolete -Dmy_rand, -Dmy_srand, and -Drandbits from ccflags." >&4
109 ccflags="`echo $ccflags | sed -e 's/-Dmy_rand=random/ /'`"
110 ccflags="`echo $ccflags | sed -e 's/-Dmy_srand=srandom/ /'`"
111 ccflags="`echo $ccflags | sed -e 's/-Drandbits=[0-9][0-9]*/ /'`"
115 while $test "$cont"; do
116 rp="Use which function to generate random numbers?"
118 ?X: Invalidate randbits if the answer is not the default so
119 ?X: that the value stored in config.sh will not be used when
120 ?X: we change our random function.
121 if $test "$ans" = "$dflt"; then
127 if set $ans val -f; eval $csym; $val; then
131 rp="I cannot find function $ans. Use that name anyway?"
150 echo "Checking to see how many bits your $randfunc() function produces..." >&4
164 register unsigned long tmp;
165 register unsigned long max = 0L;
167 for (i = 1000; i; i--) {
168 tmp = (unsigned long) $randfunc();
169 if (tmp > max) max = tmp;
171 for (i = 0; max; i++)
177 if eval $compile_ok; then
181 echo "(I can't seem to compile the test program...)"
188 rp="How many bits does your $randfunc() function produce?"
192 drand01="($randfunc() / (double) ((unsigned long)1 << $randbits))"
193 seedfunc="s$randfunc"
194 randseedtype=unsigned
196 ?X: The following is provided just in case...
199 rp="How many bits does your $randfunc() function produce?"
202 seedfunc="s$randfunc"
203 drand01="($randfunc() / (double) ((unsigned long)1 << $randbits))"
204 if set $seedfunc val -f; eval $csym; $val; then
205 echo "(Using $seedfunc() to seed random generator)"
207 echo "(Warning: no $seedfunc() to seed random generator)"
210 randseedtype=unsigned