This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Major *try* cleanup: introduce $rm_try
[metaconfig.git] / U / compline / randfunc.U
1 ?RCS: $Id: randfunc.U,v 3.0 1993/08/18 12:09:39 ram Exp $
2 ?RCS:
3 ?RCS: Copyright (c) 1991-1993, Raphael Manfredi
4 ?RCS: 
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.
10 ?RCS:
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.
14 ?RCS:
15 ?X:
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.
18 ?X:
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
24 ?X:
25 ?MAKE:randfunc drand01 seedfunc randbits randseedtype: \
26         cat rm_try test i_stdlib i_unistd Compile Myread Csym ccflags
27 ?MAKE:  -pick add $@ %<
28 ?S:randfunc:
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).
33 ?S:.
34 ?S:drand01:
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.
40 ?S:.
41 ?S:randseedtype:
42 ?S:     Indicates the type of the argument of the seedfunc.
43 ?S:.
44 ?S:seedfunc:
45 ?S:     Indicates the random number generating seed function.
46 ?S:     Values include srand48, srandom, and srand.
47 ?S:.
48 ?S:randbits:
49 ?S:     Indicates how many bits are produced by the function used to
50 ?S:     generate normalized random numbers.
51 ?S:.
52 ?C:RANDFUNC:
53 ?C:     This symbol defines the random function used to define Drand01().
54 ?C:.
55 ?C: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.
61 ?C:.
62 ?C:Rand_seed_t:
63 ?C:     This symbol defines the type of the argument of the
64 ?C:     random seed function.
65 ?C:.
66 ?C:seedDrand01:
67 ?C:     This symbol defines the macro to be used in seeding the
68 ?C:     random number generator (see Drand01).
69 ?C:.
70 ?C:RANDBITS:
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.
74 ?C:.
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               /**/
80 ?H:.
81 ?T:cont val
82 ?LINT:change ccflags
83 : How can we generate normalized random numbers ?
84 echo " "
85 echo "Looking for a random number function..." >&4
86 case "$randfunc" in
87 '')
88         if set drand48 val -f; eval $csym; $val; then
89                 dflt="drand48"
90                 echo "Good, found drand48()." >&4
91         elif set random val -f; eval $csym; $val; then
92                 dflt="random"
93                 echo "OK, found random()." >&4
94         else
95                 dflt="rand"
96                 echo "Yick, looks like I have to use rand()." >&4
97         fi
98         echo " "
99         ;;
100 *)
101         dflt="$randfunc"
102         ;;
103 esac
104 cont=true
105
106 case "$ccflags" in
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]*/ /'`"
112         ;;
113 esac
114
115 while $test "$cont"; do
116         rp="Use which function to generate random numbers?"
117         . ./myread
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
122                 : null
123         else
124                 randbits=''
125         fi
126         randfunc="$ans"
127         if set $ans val -f; eval $csym; $val; then
128                 cont=''
129         else
130                 dflt=y
131                 rp="I cannot find function $ans. Use that name anyway?"
132                 . ./myread
133                 dflt=rand
134                 case "$ans" in
135                         [yY]*) cont='';;
136                 esac
137         fi
138         case "$cont" in
139         '')
140                 case "$randfunc" in
141                 drand48)
142                         drand01="drand48()"
143                         seedfunc="srand48"
144                         randbits=48
145                         randseedtype=long
146                         ;;
147                 rand|random)
148                         case "$randbits" in
149                         '')
150 echo "Checking to see how many bits your $randfunc() function produces..." >&4
151                                 $cat >try.c <<EOCP
152 #$i_unistd I_UNISTD
153 #$i_stdlib I_STDLIB
154 #include <stdio.h>
155 #ifdef I_UNISTD
156 #  include <unistd.h>
157 #endif
158 #ifdef I_STDLIB
159 #  include <stdlib.h>
160 #endif
161 int main()
162 {
163         register int i;
164         register unsigned long tmp;
165         register unsigned long max = 0L;
166
167         for (i = 1000; i; i--) {
168                 tmp = (unsigned long) $randfunc();
169                 if (tmp > max) max = tmp;
170         }
171         for (i = 0; max; i++)
172                 max /= 2;
173         printf("%d\n",i);
174 }
175 EOCP
176                                 set try
177                                 if eval $compile_ok; then
178                                         dflt=`try`
179                                 else
180                                         dflt='?'
181                                         echo "(I can't seem to compile the test program...)"
182                                 fi
183                                 ;;
184                         *)
185                                 dflt="$randbits"
186                                 ;;
187                         esac
188                         rp="How many bits does your $randfunc() function produce?"
189                         . ./myread
190                         randbits="$ans"
191                         $rm_try
192                         drand01="($randfunc() / (double) ((unsigned long)1 << $randbits))"
193                         seedfunc="s$randfunc"
194                         randseedtype=unsigned
195                         ;;
196 ?X:     The following is provided just in case...
197                 *)
198                         dflt="31"
199                         rp="How many bits does your $randfunc() function produce?"
200                         . ./myread
201                         randbits="$ans"
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)"
206                         else
207                                 echo "(Warning: no $seedfunc() to seed random generator)"
208                                 seedfunc=rand
209                         fi
210                         randseedtype=unsigned
211                         ;;
212                 esac
213                 ;;
214         esac
215 done
216