1*0Sstevel@tonic-gate#!./perl -w 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gateBEGIN { 4*0Sstevel@tonic-gate chdir "t" if -d "t"; 5*0Sstevel@tonic-gate @INC = qw(. ../lib); 6*0Sstevel@tonic-gate} 7*0Sstevel@tonic-gate 8*0Sstevel@tonic-gate# Test srand. 9*0Sstevel@tonic-gate 10*0Sstevel@tonic-gateuse strict; 11*0Sstevel@tonic-gate 12*0Sstevel@tonic-gaterequire "test.pl"; 13*0Sstevel@tonic-gateplan(tests => 4); 14*0Sstevel@tonic-gate 15*0Sstevel@tonic-gate# Generate a load of random numbers. 16*0Sstevel@tonic-gate# int() avoids possible floating point error. 17*0Sstevel@tonic-gatesub mk_rand { map int rand 10000, 1..100; } 18*0Sstevel@tonic-gate 19*0Sstevel@tonic-gate 20*0Sstevel@tonic-gate# Check that rand() is deterministic. 21*0Sstevel@tonic-gatesrand(1138); 22*0Sstevel@tonic-gatemy @first_run = mk_rand; 23*0Sstevel@tonic-gate 24*0Sstevel@tonic-gatesrand(1138); 25*0Sstevel@tonic-gatemy @second_run = mk_rand; 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gateok( eq_array(\@first_run, \@second_run), 'srand(), same arg, same rands' ); 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate# Check that different seeds provide different random numbers 31*0Sstevel@tonic-gatesrand(31337); 32*0Sstevel@tonic-gate@first_run = mk_rand; 33*0Sstevel@tonic-gate 34*0Sstevel@tonic-gatesrand(1138); 35*0Sstevel@tonic-gate@second_run = mk_rand; 36*0Sstevel@tonic-gate 37*0Sstevel@tonic-gateok( !eq_array(\@first_run, \@second_run), 38*0Sstevel@tonic-gate 'srand(), different arg, different rands' ); 39*0Sstevel@tonic-gate 40*0Sstevel@tonic-gate 41*0Sstevel@tonic-gate# Check that srand() isn't affected by $_ 42*0Sstevel@tonic-gate{ 43*0Sstevel@tonic-gate local $_ = 42; 44*0Sstevel@tonic-gate srand(); 45*0Sstevel@tonic-gate @first_run = mk_rand; 46*0Sstevel@tonic-gate 47*0Sstevel@tonic-gate srand(42); 48*0Sstevel@tonic-gate @second_run = mk_rand; 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate ok( !eq_array(\@first_run, \@second_run), 51*0Sstevel@tonic-gate 'srand(), no arg, not affected by $_'); 52*0Sstevel@tonic-gate} 53*0Sstevel@tonic-gate 54*0Sstevel@tonic-gate# This test checks whether Perl called srand for you. 55*0Sstevel@tonic-gate@first_run = `$^X -le "print int rand 100 for 1..100"`; 56*0Sstevel@tonic-gatesleep(1); # in case our srand() is too time-dependent 57*0Sstevel@tonic-gate@second_run = `$^X -le "print int rand 100 for 1..100"`; 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gateok( !eq_array(\@first_run, \@second_run), 'srand() called automatically'); 60