In Perl, Srand () provides random number seeds for use by rand (), and Rand () produces random number generators.
If Srand () is not invoked before the first call to Rand (), then the system automatically invokes Srand () for you.
Calling Srand () with the same number of identical numbers causes the same sequence of random numbers to be generated.
Examples are as follows:
Copy Code code as follows:
Srand (26);
$number 1=rand (100);
Print "$number 1\n";
Srand (26);
$number 2=rand (100);
Print "$number 2\n";
The results obtained are as follows:
f:\>perl\a.pl
0.3753662109375
0.3753662109375
If we remove the second Srand (26), the following:
Copy Code code as follows:
Srand (26);
$number 1=rand (100);
Print "$number 1\n";
$number 2=rand (100);
Print "$number 2\n";
The results obtained are as follows:
f:\>perl\a.pl
0.3753662109375
76.397705078125
F:\>
The resulting two values are different.
With a small program, the use of a subroutine, random output of 20 random values, used here, Srand (time|$$), that is, each time to give srand new seeds, so as to ensure that the random number is not the same, the same inside time function to obtain the current times, Because the time is different, so the seed is different, we get the random number is different.
Copy Code code as follows:
#!/usr/bin/perl
My $dna = ' aaccgttaatgggcatcgatgctatgcgagct ';
Srand (time|$$);
For (my $i =0; $i <20;++ $i)
{
Print randomposition ($dna), "";
}
print "\ n";
Exit
Sub Randomposition
{
My ($string) =@_;
return int rand length $string;
}
Let's give a description of the various functions of time:
print ' time () = '. Time (). " \ n "; #从1970年到现在的秒数
Print "localtime () =". LocalTime (). \ n "; #当前时间
Print "gmtime () =". Gmtime (). \ n "; #标准格林威治时间
The output results are as follows:
f:\>perl\a.pl
Time () =1350309421
LocalTime () =mon Oct 15 21:57:01 2012
Gmtime () =mon Oct 15 13:57:01 2012
F:\>