24. 樂彩號碼產生器

僅供練習用。中不中與我無干!

loto66.pl

#! /usr/bin/perl
use strict;

our @tg;

# set num scope
my $max_num = 42;
my $spe_num;
my $gen_num=6;

# init key
my $init_key;

srand( time() ^ ($$ + ($$ << 15)) );

# ask question
print "請問您要產生幾組號碼呢 ?\n";
my $set_num=<>;
chomp $set_num;


if ($set_num <= 0) { $set_num = 1; }

# gen num
my $i;
for ($i=1; $i<=$set_num; $i++) {
      my $a=gen_loto6($gen_num);
      p_loto($a);
}

# p_tg();

sub gen_loto6 {
    my $n = shift;
    my $i; my $j;
    my @temp;
    my $r=0;
    for ($i=0; $i < $n; $i++) {
	$temp[$i] = (1..42)[42 * rand];
	for ($j=0; $j<$i; $j++) {
	    if ($temp[$i]==$temp[$j]) {
		$i--; last;
	    }
	}
	my $t=$temp[$i];
	$tg[$t]++; 
    }
    return \@temp;
}


sub p_loto {
    my $r = shift;
    my @a = @$r;
    my $i;
    my @list66;
    for ($i=0; $i<=$#a; $i++) {
	if (length($a[$i])==1) { 
	    push @list66, "0$a[$i] "; 
	} else {
	    push @list66, "$a[$i] ";
	    
	}
    }
    @list66 = sort @list66;
    print "@list66\n";
}

sub p_tg {
    for ($i=1; $i<=42; $i++) {
	print "$i ===> $tg[$i]\n";
    }
}