in reply to check my logic & a random number issue

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?

One way to approach this is to strip out everything extraneous. This bisects the problem. If the symptoms go away, you have a pretty good idea which side of the line the problem lies on.

Something like the following may help you gain some insight:

use Math::Random; for ( 1 .. 100 ) { my $d1 = random_uniform_integer(1,1,6); my $d2 = random_uniform_integer(1,1,6); print $d1 + $2, "\n"; }
Does this produce results consistent with what you're seeing?

Is also helps, when asking people to review your code, to indent. Unindented code is hard to read, and many people will take one look at it and walk away.

Oh, and use strict; to avoid accidental globals like $roll.

Replies are listed 'Best First'.
Re: Re: check my logic & a random number issue
by rmckillen (Novice) on Oct 11, 2002 at 03:15 UTC
    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; }