I need someone to check my logic on this script. I am trying to simulate the net win/loss resulting from 100 games of craps. In this script, a passline bet is always made, and if there is no craps on the comeout roll, a 1X odds bet is made before the next roll. Those are the only bets. If you don't know how craps works, check out http://thewizardofodds.com/game/craps.html and read the intro, and about "the passline" and "taking the odds". It's simple and only a couple of paragraphs.

The reason I think something is wrong, is because when I simulated just 100 trials, there were a number of identical net win/loss results that occured twice. Is my logic, or random number generation the problem? THANKS!

#!/usr/bin/perl use Math::Random; use DBI; $username = "username"; $password = "password"; $database = "database"; $dbh = DBI->connect("DBI:mysql:$database", $username, $password); for($k=1;$k<=100;$k++) { $netwinnings = 0; $counttwos = 0; for($i=1;$i<=100;$i++) { $winnings = 0; $gameover = 0; $numrolls = 0; while($gameover == 0) { $numrolls++; ##if($numrolls == 2) { print "THE POINT IS $roll\n"; } $roll = rolldice(); ##print "ROLL #$numrolls $roll\n"; if($numrolls == 1) { $point = $roll; if($roll == 7 || $roll == 11) { $gameover = 1; $winnings += 1; ##print "WIN $winnings\n\n"; } elsif($roll == 2 || $roll == 3 || $roll == 12) { $gameover = 1; $winnings -= 1; ##print "LOSE $winnings\n\n"; } } else { if($roll == $point) { $gameover = 1; if($roll == 6 || $roll == 8) { $gameover = 1; $winnings += 1.2 + 1; ##print "WIN $winnings\n\n"; } elsif($roll == 5 || $roll == 9) { $gameover = 1; $winnings += 1.5 + 1; ##print "WIN $winnings\n\n"; } elsif($roll == 4 || $roll == 10) { $gameover = 1; $winnings += 2 + 1; ##print "WIN $winnings\n\n"; } } elsif($roll == 7) { $gameover = 1; $winnings -= 2; ##print "LOSE $winnings\n\n"; } } } $winnings = sprintf "%.2f", $winnings; $netwinnings += $winnings; $netwinnings = sprintf "%.2f", $netwinnings; } $sth = $dbh->prepare("SELECT value,qty FROM craps WHERE value = $netwi +nnings"); $sth->execute(); ($value,$qty) = $sth->fetchrow_array(); ##print "\n\n$value - $netwinnings\n\n"; if($value == $netwinnings) { $sth = $dbh->prepare("UPDATE craps SET qty = qty + 1 WHERE value = $ne +twinnings"); $sth->execute(); } else { $sth = $dbh->prepare("INSERT INTO craps VALUES($netwinnings,1)"); $sth->execute(); } print "NET WIN $netwinnings\n"; } $dbh->disconnect; sub rolldice() { $d1 = random_uniform_integer(1,1,6); $d2 = random_uniform_integer(1,1,6); $roll = $d1 + $d2; return $roll; }

In reply to check my logic & a random number issue by rmckillen

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.