Small optimisations, by Brandon Black
[p5sagit/p5-mst-13.2.git] / t / op / srand.t
CommitLineData
efec44ca 1#!./perl -w
2
69026470 3BEGIN {
4 chdir "t" if -d "t";
5 @INC = qw(. ../lib);
6}
7
efec44ca 8# Test srand.
9
10use strict;
69026470 11
12require "test.pl";
13plan(tests => 4);
efec44ca 14
15# Generate a load of random numbers.
16# int() avoids possible floating point error.
17sub mk_rand { map int rand 10000, 1..100; }
18
19
20# Check that rand() is deterministic.
21srand(1138);
22my @first_run = mk_rand;
23
24srand(1138);
25my @second_run = mk_rand;
26
27ok( eq_array(\@first_run, \@second_run), 'srand(), same arg, same rands' );
28
29
30# Check that different seeds provide different random numbers
31srand(31337);
32@first_run = mk_rand;
33
34srand(1138);
35@second_run = mk_rand;
36
37ok( !eq_array(\@first_run, \@second_run),
38 'srand(), different arg, different rands' );
39
40
97458a6f 41# Check that srand() isn't affected by $_
42{
43 local $_ = 42;
44 srand();
45 @first_run = mk_rand;
46
47 srand(42);
48 @second_run = mk_rand;
49
50 ok( !eq_array(\@first_run, \@second_run),
51 'srand(), no arg, not affected by $_');
52}
53
efec44ca 54# This test checks whether Perl called srand for you.
55@first_run = `$^X -le "print int rand 100 for 1..100"`;
dc459aad 56sleep(1); # in case our srand() is too time-dependent
efec44ca 57@second_run = `$^X -le "print int rand 100 for 1..100"`;
58
59ok( !eq_array(\@first_run, \@second_run), 'srand() called automatically');