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 ?MAKE:randfunc mrand seedfunc nrandbits: cat +cc rm test Myread Csym
20 ?MAKE: -pick add $@ %<
22 ?S: Indicates the name of the random number function to use.
23 ?S: Values include drand48, random, and rand. In C programs,
24 ?S: the 'nrand' macro is defined to generate uniformly distributed
25 ?S: random numbers over the range [0., 1.] (see mrand and nrand).
28 ?S: Indicates the macro to be used to generate normalized
29 ?S: random numbers. Uses randfunc, often divided by
30 ?S: (double) ((1 << nrandbits) -1) in order to normalize the result.
31 ?S: In C programs, the macro 'nrand' is maped on mrand.
34 ?S: Indicates the random number generating seed function.
35 ?S: Values include srand48, srandom, and srand.
38 ?S: Indicates how many bits are produced by the function used to
39 ?S: generate normalized random numbers.
42 ?C: This macro is to be used to generate uniformly distributed
43 ?C: random numbers over the range [0., 1.].
46 ?C: This symbol defines the macro to be used in seeding the
47 ?C: random number generator (see nrand).
49 ?H:#define nrand() $mrand /**/
50 ?H:#define seednrand(x) $seedfunc(x) /**/
53 : How can we generate normalized random numbers ?
57 if set drand48 val -f; eval $csym; $val; then
59 echo "Good, found drand48()." >&4
60 elif set random val -f; eval $csym; $val; then
62 echo "OK, found random()." >&4
65 echo "Yick, looks like I have to use rand()." >&4
74 while $test "$cont"; do
75 rp="Use which function to generate random numbers?"
77 ?X: Invalidates nrandbits if the answer is not the default so
78 ?X: that the value stored in config.sh will not be used when
79 ?X: we change our random function.
80 if $test "$ans" = "$dflt"; then
86 if set $ans val -f; eval $csym; $val; then
90 rp="Function $ans does not exists. Use that name anyway?"
107 echo "Checking to see how many bits your $randfunc() function produces..." >&4
113 register unsigned long tmp;
114 register unsigned long max = 0L;
115 extern long random();
117 for (i = 1000; i; i--) {
118 tmp = (unsigned long)$randfunc();
119 if (tmp > max) max = tmp;
121 for (i = 0; max; i++)
126 if $cc -o try try.c >/dev/null 2>&1 ; then
130 echo "(I can't seem to compile the test program...)"
137 rp="How many bits does your $randfunc() function produce?"
141 mrand="($randfunc() / (double) ((1 << $nrandbits) -1))"
144 ?X: The following is provided just in case...
147 rp="How many bits does your $randfunc() function produce?"
150 seedfunc="s$randfunc"
151 mrand="($randfunc() / (double) ((1 << $nrandbits) -1))"
152 if set $seedfunc val -f; eval $csym; $val; then
153 echo "(Using $seedfunc() to seed random generator)"
155 echo "(Warning: no $seedfunc() to seed random generator)"