in reply to Re: check my logic & a random number issue
in thread check my logic & a random number issue

Here is the indented code. Just stripping down to the one simple for loop doesn't help because it will just print a number 1-12, 100 times. That won't really allow me to observe any sort of pattern or inconsistencies, while the output with all of the other code thrown in there gives values like xx.xx, which can range from negative hundreds of dollars to positive hundreds of dollars.
#!/usr/bin/perl use Math::Random; use DBI; use strict; $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 = $n +etwinnings"); $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 val +ue = $netwinnings"); $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; }