Commit | Line | Data |
efec44ca |
1 | #!./perl -w |
2 | |
3 | # Test srand. |
4 | |
5 | use strict; |
97458a6f |
6 | use Test::More tests => 4; |
efec44ca |
7 | |
8 | # Generate a load of random numbers. |
9 | # int() avoids possible floating point error. |
10 | sub mk_rand { map int rand 10000, 1..100; } |
11 | |
12 | |
13 | # Check that rand() is deterministic. |
14 | srand(1138); |
15 | my @first_run = mk_rand; |
16 | |
17 | srand(1138); |
18 | my @second_run = mk_rand; |
19 | |
20 | ok( eq_array(\@first_run, \@second_run), 'srand(), same arg, same rands' ); |
21 | |
22 | |
23 | # Check that different seeds provide different random numbers |
24 | srand(31337); |
25 | @first_run = mk_rand; |
26 | |
27 | srand(1138); |
28 | @second_run = mk_rand; |
29 | |
30 | ok( !eq_array(\@first_run, \@second_run), |
31 | 'srand(), different arg, different rands' ); |
32 | |
33 | |
97458a6f |
34 | # Check that srand() isn't affected by $_ |
35 | { |
36 | local $_ = 42; |
37 | srand(); |
38 | @first_run = mk_rand; |
39 | |
40 | srand(42); |
41 | @second_run = mk_rand; |
42 | |
43 | ok( !eq_array(\@first_run, \@second_run), |
44 | 'srand(), no arg, not affected by $_'); |
45 | } |
46 | |
efec44ca |
47 | # This test checks whether Perl called srand for you. |
48 | @first_run = `$^X -le "print int rand 100 for 1..100"`; |
49 | @second_run = `$^X -le "print int rand 100 for 1..100"`; |
50 | |
51 | ok( !eq_array(\@first_run, \@second_run), 'srand() called automatically'); |