Perl random number

This is a bit more complex solution to generate a random number in Perl:

use strict;
sub num {
    my $ret = '';
    while($_[0] =~ /(\d)/g) {
        $ret .= $1;
    }
    return($ret);
}

sub myshuffle {
    my @str = ($#_ == 0)?split(//o, $_[0]):@_;
    my $r1 = 0;
    my $r2 = 0;
    for(0..($#str*2)){
        $r1 = int(rand($#str+1));
        $r2 = int(rand($#str+1))while($r1 == $r2 && $#str>4);
        ($str[$r1],$str[$r2]) = ($str[$r2],$str[$r1]);
    }
    return join '', @str;
}

sub myrand {
    my $c = 0;
    my $s = '';
    my $r1 = 0;
    my $r2 = 0;
    for(1..100){
        $r1 = int(rand(10+$c));
        $r2 = int(rand(10+$c))while($r1 == $r2);
        $s = crypt(myshuffle($r2.$s.$c.$r1),int($c+rand(99999)));
        $c = num($s);
    }
   return($c); 
}


print myrand().$/;

It has no max value but normal the highest is 99999.

 

 

Leave a Reply

Your email address will not be published.

fourteen + one =