This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Populate metaconfig branch.
[metaconfig.git] / dist-3.0at70b / mcon / U / 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 ?MAKE:randfunc mrand seedfunc nrandbits: cat +cc rm test Myread Csym
20 ?MAKE:  -pick add $@ %<
21 ?S:randfunc:
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).
26 ?S:.
27 ?S:mrand:
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.
32 ?S:.
33 ?S:seedfunc:
34 ?S:     Indicates the random number generating seed function.
35 ?S:     Values include srand48, srandom, and srand.
36 ?S:.
37 ?S:nrandbits:
38 ?S:     Indicates how many bits are produced by the function used to
39 ?S:     generate normalized random numbers.
40 ?S:.
41 ?C:nrand:
42 ?C:     This macro is to be used to generate uniformly distributed
43 ?C:     random numbers over the range [0., 1.].
44 ?C:.
45 ?C:seednrand:
46 ?C:     This symbol defines the macro to be used in seeding the
47 ?C:     random number generator (see nrand).
48 ?C:.
49 ?H:#define nrand()              $mrand          /**/
50 ?H:#define seednrand(x) $seedfunc(x)    /**/
51 ?H:.
52 ?T:cont val
53 : How can we generate normalized random numbers ?
54 echo " "
55 case "$randfunc" in
56 '')
57         if set drand48 val -f; eval $csym; $val; then
58                 dflt="drand48"
59                 echo "Good, found drand48()." >&4
60         elif set random val -f; eval $csym; $val; then
61                 dflt="random"
62                 echo "OK, found random()." >&4
63         else
64                 dflt="rand"
65                 echo "Yick, looks like I have to use rand()." >&4
66         fi
67         echo " "
68         ;;
69 *)
70         dflt="$randfunc"
71         ;;
72 esac
73 cont=true
74 while $test "$cont"; do
75         rp="Use which function to generate random numbers?"
76         . ./myread
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
81                 : null
82         else
83                 nrandbits=''
84         fi
85         randfunc="$ans"
86         if set $ans val -f; eval $csym; $val; then
87                 cont=''
88         else
89                 dflt=n
90                 rp="Function $ans does not exists. Use that name anyway?"
91                 . ./myread
92                 dflt=rand
93                 case "$ans" in
94                         [yY]*) cont='';;
95                 esac
96         fi
97         case "$cont" in
98         '')
99                 case "$randfunc" in
100                 drand48)
101                         mrand="drand48()"
102                         seedfunc="srand48"
103                         ;;
104                 rand*)
105                         case "$nrandbits" in
106                         '')
107 echo "Checking to see how many bits your $randfunc() function produces..." >&4
108                         $cat >try.c <<EOCP
109 #include <stdio.h>
110 main()
111 {
112         register int i;
113         register unsigned long tmp;
114         register unsigned long max = 0L;
115         extern long random();
116
117         for (i = 1000; i; i--) {
118                 tmp = (unsigned long)$randfunc();
119                 if (tmp > max) max = tmp;
120         }
121         for (i = 0; max; i++)
122                 max /= 2;
123         printf("%d\n",i);
124 }
125 EOCP
126                         if $cc -o try try.c >/dev/null 2>&1 ; then
127                         dflt=`try`
128                         else
129                         dflt='?'
130                         echo "(I can't seem to compile the test program...)"
131                         fi
132                         ;;
133                         *)
134                                 dflt="$nrandbits"
135                                 ;;
136                         esac
137                         rp="How many bits does your $randfunc() function produce?"
138                         . ./myread
139                         nrandbits="$ans"
140                         $rm -f try.c try
141                         mrand="($randfunc() / (double) ((1 << $nrandbits) -1))"
142                         seedfunc="srand"
143                         ;;
144 ?X:     The following is provided just in case...
145                 *)
146                         dflt="31"
147                         rp="How many bits does your $randfunc() function produce?"
148                         . ./myread
149                         nrandbits="$ans"
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)"
154                         else
155                                 echo "(Warning: no $seedfunc() to seed random generator)"
156                                 seedfunc=rand
157                         fi
158                         ;;
159                 esac
160                 ;;
161         esac
162 done
163