This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Forgot to add a file with Change #11840
[perl5.git] / t / op / srand.t
CommitLineData
efec44ca
AB
1#!./perl -w
2
3# Test srand.
4
5use strict;
6use Test::More tests => 5;
7
8# Generate a load of random numbers.
9# int() avoids possible floating point error.
10sub mk_rand { map int rand 10000, 1..100; }
11
12
13# Check that rand() is deterministic.
14srand(1138);
15my @first_run = mk_rand;
16
17srand(1138);
18my @second_run = mk_rand;
19
20ok( eq_array(\@first_run, \@second_run), 'srand(), same arg, same rands' );
21
22
23# Check that different seeds provide different random numbers
24srand(31337);
25@first_run = mk_rand;
26
27srand(1138);
28@second_run = mk_rand;
29
30ok( !eq_array(\@first_run, \@second_run),
31 'srand(), different arg, different rands' );
32
33
34# Check that srand() with no args provides different seeds.
35srand();
36@first_run = mk_rand;
37
38srand();
39@second_run = mk_rand;
40
41ok( !eq_array(\@first_run, \@second_run), 'srand(), no arg, different rands');
42
43
44# Check that srand() isn't effected by $_
45{
46 local $_ = 42;
47 srand();
48 @first_run = mk_rand;
49
50 srand();
51 @second_run = mk_rand;
52
53 ok( !eq_array(\@first_run, \@second_run),
54 'srand(), no arg, not effected by $_');
55}
56
57
58
59# This test checks whether Perl called srand for you.
60@first_run = `$^X -le "print int rand 100 for 1..100"`;
61@second_run = `$^X -le "print int rand 100 for 1..100"`;
62
63ok( !eq_array(\@first_run, \@second_run), 'srand() called automatically');