This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
More tests
[perl5.git] / t / op / rand.t
CommitLineData
a0d0e21e
LW
1#!./perl
2
748a9306 3# From: kgb@ast.cam.ac.uk (Karl Glazebrook)
a0d0e21e 4
3524d3b9 5print "1..6\n";
a0d0e21e 6
748a9306 7srand;
a0d0e21e 8
3524d3b9 9$m=$max=0;
748a9306
LW
10for(1..1000){
11 $n = rand(1);
3524d3b9 12 if ($n<0) {
748a9306
LW
13 print "not ok 1\n# The value of randbits is likely too low in config.sh\n";
14 exit
15 }
16 $m += $n;
3524d3b9 17 $max = $n if $n > $max;
748a9306
LW
18}
19$m=$m/1000;
20print "ok 1\n";
21
3524d3b9
TP
22$off = log($max)/log(2);
23if ($off > 0) { $off = int(.5+$off) }
24 else { $off = - int(.5-$off) }
25print "# Consider adding $off to randbits\n" if $off > 0;
26print "# Consider subtracting ", -$off, " from randbits\n" if $off < 0;
27
748a9306
LW
28if ($m<0.4) {
29 print "not ok 2\n# The value of randbits is likely too high in config.sh\n";
30}
31elsif ($m>0.6) {
3524d3b9 32 print "not ok 2\n# The value of randbits is likely too low in config.sh\n";
748a9306
LW
33}else{
34 print "ok 2\n";
a0d0e21e
LW
35}
36
748a9306 37srand;
a0d0e21e 38
748a9306
LW
39$m=0;
40for(1..1000){
41 $n = rand(100);
42 if ($n<0 || $n>=100) {
43 print "not ok 3\n";
44 exit
45 }
46 $m += $n;
47
48}
49$m=$m/1000;
50print "ok 3\n";
51
52if ($m<40 || $m>60) {
53 print "not ok 4\n";
54}else{
55 print "ok 4\n";
a0d0e21e
LW
56}
57
3524d3b9
TP
58srand(3.14159);
59$r = rand;
60srand(3.14159);
61print "# srand is not consistent.\nnot " if rand != $r;
62print "ok 5\n";
63
64print "# rand is unchanging!\nnot " if rand == $r;
65print "ok 6\n";
748a9306 66