summaryrefslogtreecommitdiff
path: root/mcon/U/randfunc.U
blob: 731c9159857381dd8c5a4fc38ef66dee2d424631 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
?RCS: $Id: randfunc.U 167 2013-05-08 17:58:00Z rmanfredi $
?RCS:
?RCS: Copyright (c) 1991-1997, 2004-2006, Raphael Manfredi
?RCS: 
?RCS: You may redistribute only under the terms of the Artistic License,
?RCS: as specified in the README file that comes with the distribution.
?RCS: You may reuse parts of this distribution only within the terms of
?RCS: that same Artistic License; a copy of which may be found at the root
?RCS: of the source tree for dist 4.0.
?RCS:
?RCS: $Log: randfunc.U,v $
?RCS: Revision 3.0  1993/08/18  12:09:39  ram
?RCS: Baseline for dist 3.0 netwide release.
?RCS:
?X:
?X:	This is the new unit that should be used when random
?X:	functions are to be used. It thus makes randbits.U obsolete.
?X:
?MAKE:randfunc mrand seedfunc nrandbits: cat +cc rm test Myread Csym
?MAKE:	-pick add $@ %<
?S:randfunc:
?S:	Indicates the name of the random number function to use.
?S:	Values include drand48, random, and rand. In C programs,
?S:	the 'nrand' macro is defined to generate uniformly distributed
?S:	random numbers over the range [0., 1.] (see mrand and nrand).
?S:.
?S:mrand:
?S:	Indicates the macro to be used to generate normalized
?S:	random numbers.  Uses randfunc, often divided by
?S:	(double) ((1 << nrandbits) -1) in order to normalize the result.
?S:	In C programs, the macro 'nrand' is maped on mrand.
?S:.
?S:seedfunc:
?S:	Indicates the random number generating seed function.
?S:	Values include srand48, srandom, and srand.
?S:.
?S:nrandbits:
?S:	Indicates how many bits are produced by the function used to
?S:	generate normalized random numbers.
?S:.
?C:nrand:
?C:	This macro is to be used to generate uniformly distributed
?C:	random numbers over the range [0., 1.].
?C:.
?C:seednrand:
?C:	This symbol defines the macro to be used in seeding the
?C:	random number generator (see nrand).
?C:.
?H:#define nrand()		$mrand		/**/
?H:#define seednrand(x)	$seedfunc(x)	/**/
?H:.
?T:cont val
?LINT:nothere $nrandbits)
: How can we generate normalized random numbers ?
echo " "
case "$randfunc" in
'')
	if set drand48 val -f; eval $csym; $val; then
		dflt="drand48"
		echo "Good, found drand48()." >&4
	elif set random val -f; eval $csym; $val; then
		dflt="random"
		echo "OK, found random()." >&4
	else
		dflt="rand"
		echo "Yick, looks like I have to use rand()." >&4
	fi
	echo " "
	;;
*)
	dflt="$randfunc"
	;;
esac
cont=true
while $test "$cont"; do
	rp="Use which function to generate random numbers?"
	. ./myread
?X: Invalidates nrandbits if the answer is not the default so
?X:	that the value stored in config.sh will not be used when
?X:	we change our random function.
	if $test "$ans" = "$dflt"; then
		: null
	else
		nrandbits=''
	fi
	randfunc="$ans"
	if set $ans val -f; eval $csym; $val; then
		cont=''
	else
		dflt=n
		rp="Function $ans does not exists. Use that name anyway?"
		. ./myread
		dflt=rand
		case "$ans" in
			[yY]*) cont='';;
		esac
	fi
	case "$cont" in
	'')
		case "$randfunc" in
		drand48)
			mrand="drand48()"
			seedfunc="srand48"
			;;
		rand*)
			case "$nrandbits" in
			'')
echo "Checking to see how many bits your $randfunc() function produces..." >&4
			$cat >try.c <<EOCP
#include <stdio.h>
int main()
{
	register int i;
	register unsigned long tmp;
	register unsigned long max = 0L;
	extern long random();

	for (i = 1000; i; i--) {
		tmp = (unsigned long)$randfunc();
		if (tmp > max) max = tmp;
	}
	for (i = 0; max; i++)
		max /= 2;
	printf("%d\n",i);
}
EOCP
			if $cc -o try try.c >/dev/null 2>&1 ; then
			dflt=`try`
			else
			dflt='?'
			echo "(I can't seem to compile the test program...)"
			fi
			;;
			*)
				dflt="$nrandbits"
				;;
			esac
			rp="How many bits does your $randfunc() function produce?"
			. ./myread
			nrandbits="$ans"
			$rm -f try.c try
			mrand="($randfunc() / (double) ((1 << $nrandbits) -1))"
			seedfunc="srand"
			;;
?X:	The following is provided just in case...
		*)
			dflt="31"
			rp="How many bits does your $randfunc() function produce?"
			. ./myread
			nrandbits="$ans"
			seedfunc="s$randfunc"
			mrand="($randfunc() / (double) ((1 << $nrandbits) -1))"
			if set $seedfunc val -f; eval $csym; $val; then
				echo "(Using $seedfunc() to seed random generator)"
			else
				echo "(Warning: no $seedfunc() to seed random generator)"
				seedfunc=rand
			fi
			;;
		esac
		;;
	esac
done