Craps Simulator in Perl

You can easily adjust for your own use.

#!/usr/bin/perl

#  How many games would you like to roll?
$iterations = 1000;

for ($count = $iterations; $count >= 1; $count--) {

print "$count \n";

# Bankroll per game.  Rules are set up for a $5 bet on a 3x/4x/5x table.
$bankroll = 200;

$rollcount = 0;
$high = 200;

$itson = 1;

while ($itson == 1) {

	$die1 = int(rand() * 6) + 1;
	$die2 = int(rand() * 6) + 1;
	$roll = $die1 + $die2;
	$rollcount = $rollcount + 1;

	if ($roll == 7 || $roll == 11) {
		$bankroll = $bankroll + 5;
		# print "W $roll $bankroll $rollcount\n";
		if ($bankroll > $high) {
			$high = $bankroll;
			}
		}

	elsif ($roll == 2 || $roll == 3 || $roll == 12) {
		$bankroll = $bankroll - 5;
		# print "L $roll $bankroll $rollcount\n";
		}

	else {
		$setnum = $roll;
		$notcrapped = 1;
		while ($notcrapped == 1) {

			$die1 = int(rand() * 6) + 1;
			$die2 = int(rand() * 6) + 1;
			$roll = $die1 + $die2;
			$rollcount = $rollcount + 1;

			if ($roll == $setnum) {
				$bankroll = $bankroll + 35;
				# print "W $roll $bankroll $rollcount\n";
				if ($bankroll > $high) {
					$high = $bankroll;
					}
				$notcrapped = 0;
				}

			if ($roll == 7) {

				if ($setnum == 4 || $setnum == 10) {
					$bankroll = $bankroll - 20;
					}

				if ($setnum == 5 || $setnum == 9) {
					$bankroll = $bankroll - 25;
					}

				if ($setnum == 6 || $setnum == 8) {
					$bankroll = $bankroll - 30;
					}

				# print "L $roll $bankroll $rollcount\n";
				$notcrapped = 0;
				}
			}
		}	

# Game ends when you have less than $5
	if ($bankroll < 5) {
		$losses = $losses + 1;
		$itson = 0;
		}

# It also ends if youmake $1000
	if ($bankroll >= 1000) {
		$wins = $wins + 1;
		$itson = 0;
		}

	}

if ($high > $biggest) {
	$biggest = $high;
	}

if ($rollcount > $mostrolls) {
	$mostrolls = $rollcount;
	}

$bigrollcount = $bigrollcount + $rollcount;
$bighigh = $bighigh + $high;

}

print "$iterations Iterations \n";
print "Ave Rollcounts: " . sprintf("%.3f", $bigrollcount/$iterations) . "\n";
print "Ave Rollcounts Hours: " . sprintf("%.3f", int($bigrollcount/$iterations)/180) . "\n";
print "Most Rollcounts: $mostrolls\n";
# Most Rollcounts Days assumes 3 rolls/minute.
print "Most Rollcounts Days: " . sprintf("%.3f", ($mostrolls/180)/24) ."\n";
print "Ave Most Won: " . ($bighigh/$iterations) . "\n";
print "Biggest Win: $biggest\n";
print "$wins Wins | $losses Losses";

About this entry