Partial cleanup
[p5sagit/p5-mst-13.2.git] / t / op / rand.t
1 #!./perl
2
3 # From Tom Phoenix <rootbeer@teleport.com> 22 Feb 1997
4 # Based upon a test script by kgb@ast.cam.ac.uk (Karl Glazebrook)
5
6 # Looking for the hints? You're in the right place. 
7 # The hints are near each test, so search for "TEST #", where
8 # the pound sign is replaced by the number of the test.
9
10 # I'd like to include some more robust tests, but anything
11 # too subtle to be detected here would require a time-consuming
12 # test. Also, of course, we're here to detect only flaws in Perl;
13 # if there are flaws in the underlying system rand, that's not
14 # our responsibility. But if you want better tests, see
15 # The Art of Computer Programming, Donald E. Knuth, volume 2,
16 # chapter 3. ISBN 0-201-03822-6 (v. 2)
17
18 BEGIN {
19     chdir "t" if -d "t";
20     @INC = '../lib';
21 }
22
23 use strict;
24 use Config;
25
26 print "1..11\n";
27
28 my $reps = 10000;       # How many times to try rand each time.
29                         # May be changed, but should be over 500.
30                         # The more the better! (But slower.)
31
32 sub bits ($) {
33     # Takes a small integer and returns the number of one-bits in it.
34     my $total;
35     my $bits = sprintf "%o", $_[0];
36     while (length $bits) {
37         $total += (0,1,1,2,1,2,2,3)[chop $bits];        # Oct to bits
38     }
39     $total;
40 }
41
42 # First, let's see whether randbits is set right
43 {
44     my($max, $min, $sum);       # Characteristics of rand
45     my($off, $shouldbe);        # Problems with randbits
46     my($dev, $bits);            # Number of one bits
47     my $randbits = $Config{randbits};
48     $max = $min = rand(1);
49     for (1..$reps) {
50         my $n = rand(1);
51         if ($n < 0.0 or $n >= 1.0) {
52             print <<EOM;
53 # WHOA THERE!  \$Config{drand01} is set to '$Config{drand01}',
54 # but that apparently produces values < 0.0 or >= 1.0.
55 # Make sure \$Config{drand01} is a valid expression in the
56 # C-language, and produces values in the range [0.0,1.0).
57 #
58 # I give up.
59 EOM
60             exit;
61         }
62         $sum += $n;
63         $bits += bits($n * 256);        # Don't be greedy; 8 is enough
64                     # It's too many if randbits is less than 8!
65                     # But that should never be the case... I hope.
66                     # Note: If you change this, you must adapt the
67                     # formula for absolute standard deviation, below.
68         $max = $n if $n > $max;
69         $min = $n if $n < $min;
70     }
71
72
73     # Hints for TEST 1
74     #
75     # This test checks for one of Perl's most frequent
76     # mis-configurations. Your system's documentation
77     # for rand(2) should tell you what value you need
78     # for randbits. Usually the diagnostic message
79     # has the right value as well. Just fix it and
80     # recompile, and you'll usually be fine. (The main 
81     # reason that the diagnostic message might get the
82     # wrong value is that Config.pm is incorrect.)
83     #
84     if ($max <= 0 or $max >= (2 ** $randbits)) {# Just in case...
85         print "# max=[$max] min=[$min]\nnot ok 1\n";
86         print "# This perl was compiled with randbits=$randbits\n";
87         print "# which is _way_ off. Or maybe your system rand is broken,\n";
88         print "# or your C compiler can't multiply, or maybe Martians\n";
89         print "# have taken over your computer. For starters, see about\n";
90         print "# trying a better value for randbits, probably smaller.\n";
91         # If that isn't the problem, we'll have
92         # to put d_martians into Config.pm 
93         print "# Skipping remaining tests until randbits is fixed.\n";
94         exit;
95     }
96
97     $off = log($max) / log(2);                  # log2
98     $off = int($off) + ($off > 0);              # Next more positive int
99     if ($off) {
100         $shouldbe = $Config{randbits} + $off;
101         print "# max=[$max] min=[$min]\nnot ok 1\n";
102         print "# This perl was compiled with randbits=$randbits on $^O.\n";
103         print "# Consider using randbits=$shouldbe instead.\n";
104         # And skip the remaining tests; they would be pointless now.
105         print "# Skipping remaining tests until randbits is fixed.\n";
106         exit;
107     } else {
108         print "ok 1\n";
109     }
110
111     # Hints for TEST 2
112     #
113     # This should always be true: 0 <= rand(1) < 1
114     # If this test is failing, something is seriously wrong,
115     # either in perl or your system's rand function.
116     #
117     if ($min < 0 or $max >= 1) {        # Slightly redundant...
118         print "not ok 2\n";
119         print "# min too low\n" if $min < 0;
120         print "# max too high\n" if $max >= 1;
121     } else {
122         print "ok 2\n";
123     }
124
125     # Hints for TEST 3
126     #
127     # This is just a crude test. The average number produced
128     # by rand should be about one-half. But once in a while
129     # it will be relatively far away. Note: This test will
130     # occasionally fail on a perfectly good system!
131     # See the hints for test 4 to see why.
132     #
133     $sum /= $reps;
134     if ($sum < 0.4 or $sum > 0.6) {
135         print "not ok 3\n# Average random number is far from 0.5\n";
136     } else {
137         print "ok 3\n";
138     }
139
140     # Hints for TEST 4
141     #
142     #   NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
143     # This test will fail .1% of the time on a normal system.
144     #                           also
145     # This test asks you to see these hints 100% of the time!
146     #   NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
147     #
148     # There is probably no reason to be alarmed that
149     # something is wrong with your rand function. But,
150     # if you're curious or if you can't help being 
151     # alarmed, keep reading.
152     #
153     # This is a less-crude test than test 3. But it has
154     # the same basic flaw: Unusually distributed random
155     # values should occasionally appear in every good
156     # random number sequence. (If you flip a fair coin
157     # twenty times every day, you'll see it land all
158     # heads about one time in a million days, on the
159     # average. That might alarm you if you saw it happen
160     # on the first day!)
161     #
162     # So, if this test failed on you once, run it a dozen
163     # times. If it keeps failing, it's likely that your
164     # rand is bogus. If it keeps passing, it's likely
165     # that the one failure was bogus. If it's a mix,
166     # read on to see about how to interpret the tests.
167     #
168     # The number printed in square brackets is the
169     # standard deviation, a statistical measure
170     # of how unusual rand's behavior seemed. It should
171     # fall in these ranges with these *approximate*
172     # probabilities:
173     #
174     #           under 1         68.26% of the time
175     #           1-2             27.18% of the time
176     #           2-3              4.30% of the time
177     #           over 3           0.26% of the time
178     #
179     # If the numbers you see are not scattered approximately
180     # (not exactly!) like that table, check with your vendor
181     # to find out what's wrong with your rand. Or with this
182     # algorithm. :-)
183     #
184     # Calculating absoulute standard deviation for number of bits set
185     # (eight bits per rep)
186     $dev = abs ($bits - $reps * 4) / sqrt($reps * 2);
187
188     if ($dev < 1.96) {
189         print "ok 4\n";         # 95% of the time.
190         print "# Your rand seems fine. If this test failed\n";
191         print "# previously, you may want to run it again.\n";
192     } elsif ($dev < 2.575) {
193         print "ok 4\n# In here about 4% of the time. Hmmm...\n";
194         print "# This is ok, but suspicious. But it will happen\n";
195         print "# one time out of 25, more or less.\n";
196         print "# You should run this test again to be sure.\n";
197     } elsif ($dev < 3.3) {
198         print "ok 4\n# In this range about 1% of the time.\n";
199         print "# This is very suspicious. It will happen only\n";
200         print "# about one time out of 100, more or less.\n";
201         print "# You should run this test again to be sure.\n";
202     } elsif ($dev < 3.9) {
203         print "not ok 4\n# In this range very rarely.\n";
204         print "# This is VERY suspicious. It will happen only\n";
205         print "# about one time out of 1000, more or less.\n";
206         print "# You should run this test again to be sure.\n";
207     } else {
208         print "not ok 4\n# Seriously whacked.\n";
209         print "# This is VERY VERY suspicious.\n";
210         print "# Your rand seems to be bogus.\n";
211     }
212     print "#\n# If you are having random number troubles,\n";
213     print "# see the hints within the test script for more\n";
214     printf "# information on why this might fail. [ %.3f ]\n", $dev;
215 }
216
217 {
218     srand;              # These three lines are for test 7
219     my $time = time;    # It's just faster to do them here.
220     my $rand = join ", ", rand, rand, rand;
221
222     # Hints for TEST 5
223     # 
224     # This test checks that the argument to srand actually 
225     # sets the seed for generating random numbers. 
226     #
227     srand(3.14159);
228     my $r = rand;
229     srand(3.14159);
230     if (rand != $r) {
231         print "not ok 5\n";
232         print "# srand is not consistent.\n";
233     } else {
234         print "ok 5\n";
235     }
236
237     # Hints for TEST 6
238     # 
239     # This test just checks that the previous one didn't 
240     # give us false confidence!
241     #
242     if (rand == $r) {
243         print "not ok 6\n";
244         print "# rand is now unchanging!\n";
245     } else {
246         print "ok 6\n";
247     }
248
249     # Hints for TEST 7
250     #
251     # This checks that srand without arguments gives
252     # different sequences each time. Note: You shouldn't
253     # be calling srand more than once unless you know
254     # what you're doing! But if this fails on your 
255     # system, run perlbug and let the developers know
256     # what other sources of randomness srand should
257     # tap into.
258     #
259     while ($time == time) { }   # Wait for new second, just in case.
260     srand;
261     if ((join ", ", rand, rand, rand) eq $rand) {
262         print "not ok 7\n";
263         print "# srand without args isn't varying.\n";
264     } else {
265         print "ok 7\n";
266     }
267 }
268
269 # Now, let's see whether rand accepts its argument
270 {
271     my($max, $min);
272     $max = $min = rand(100);
273     for (1..$reps) {
274         my $n = rand(100);
275         $max = $n if $n > $max;
276         $min = $n if $n < $min;
277     }
278
279     # Hints for TEST 8
280     #
281     # This test checks to see that rand(100) really falls 
282     # within the range 0 - 100, and that the numbers produced
283     # have a reasonably-large range among them.
284     #
285     if ($min < 0 or $max >= 100 or ($max - $min) < 65) {
286         print "not ok 8\n";
287         print "# min too low\n" if $min < 0;
288         print "# max too high\n" if $max >= 100;
289         print "# range too narrow\n" if ($max - $min) < 65;
290     } else {
291         print "ok 8\n";
292     }
293
294     # Hints for TEST 9
295     #
296     # This test checks that rand without an argument
297     # is equivalent to rand(1).
298     #
299     $_ = 12345;         # Just for fun.
300     srand 12345;
301     my $r = rand;
302     srand 12345;
303     if (rand(1) == $r) {
304         print "ok 9\n";
305     } else {
306         print "not ok 9\n";
307         print "# rand without arguments isn't rand(1)!\n";
308     }
309
310     # Hints for TEST 10
311     #
312     # This checks that rand without an argument is not
313     # rand($_). (In case somebody got overzealous.)
314     # 
315     if ($r >= 1) {
316         print "not ok 10\n";
317         print "# rand without arguments isn't under 1!\n";
318     } else {
319         print "ok 10\n";
320     }
321 }
322
323 # Hints for TEST 11
324 #
325 # This test checks whether Perl called srand for you. This should
326 # be the case in version 5.004 and later. Note: You must still
327 # call srand if your code might ever be run on a pre-5.004 system!
328 #
329 AUTOSRAND:
330 {
331     unless ($Config{d_fork}) {
332         # Skip this test. It's not likely to be system-specific, anyway.
333         print "ok 11\n# Skipping this test on this platform.\n";
334         last;
335     }
336
337     my($pid, $first);
338     for (1..5) {
339         my $PERL = (($^O eq 'VMS') ? "MCR $^X"
340                     : ($^O eq 'MSWin32') ? '.\perl'
341                     : ($^O eq 'NetWare') ? 'perl'
342                     : './perl');
343         $pid = open PERL, qq[$PERL -e "print rand"|];
344         die "Couldn't pipe from perl: $!" unless defined $pid;
345         if (defined $first) {
346             if ($first ne <PERL>) {
347                 print "ok 11\n";
348                 last AUTOSRAND;
349             }
350         } else {
351             $first = <PERL>;
352         }
353         close PERL or die "perl returned error code $?";
354     }
355     print "not ok 11\n# srand isn't being autocalled.\n";
356 }