#!/usr/local/bin/perl

# Seed the RNG by using the Checksum of the gzipped output of ps
# Also referred to as "collecting entropy"
srand (time ^ $$ ^ unpack "%32L*", `ps auxw | gzip`);

$ranks = 13;
@suits = ("Spades","Hearts","Diamonds","Clubs");
foreach $suit (@suits) { $suitnum++ ; }
$numcards = $ranks * $suitnum;

foreach $n (1..$ranks) {
        foreach $s (@suits) {
			@deck[$i] = "$n of $s";
			$i++;
	}
}

# A fair, linear shuffling sequence
@shuffled = ();			# an empty array
while ($numcards > 0) {
	push(@shuffled, splice(@deck, (rand $numcards), 1, $deck[$numcards-1]));
	pop(@deck);  		# optional (keeps $numcards == number of cards in @deck)
	$numcards--;
}

# Print the deck in newly shuffled order
foreach $shuffled (@shuffled) { 
	@shufcard = split ' ', $shuffled ; 
	SWITCH: {
		if (@shufcard[0] == 1) { print "A @shufcard[1..2] \n" ; last SWITCH ; } 
		if (@shufcard[0] == 10) { print "T @shufcard[1..2] \n" ; last SWITCH ; } 
		if (@shufcard[0] == 11) { print "J @shufcard[1..2] \n" ; last SWITCH ; } 
		if (@shufcard[0] == 12) { print "Q @shufcard[1..2] \n" ; last SWITCH ; } 
		if (@shufcard[0] == 13) { print "K @shufcard[1..2] \n" ; last SWITCH ; } 
		print "@shufcard[0..2] \n" ;
		}
}