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?
Other than just eyeballing the results, what statistical analyses have you performed? Have you checked that your average dice roll is 7? When I added code in to print the average dice roll, I noticed that there's a condition that you haven't covered.
if ($roll == $point) { $gameover = 1; if ($roll == 6 || $roll == 8) { $gameover = 1; #not necessary $winnings += 1.2 + 1; print "WIN $winnings\n"; } elsif ($roll == 5 || $roll == 9) { $gameover = 1; # not necessary $winnings += 1.5 + 1; print "WIN $winnings\n"; } elsif ($roll == 4 || $roll == 10) { $gameover = 1; # not necessary $winnings += 2 + 1; print "WIN $winnings\n"; } else { print "what happens here?"; # loss? draw? } }
Because you put $gameover = 1 at the start of that conditional block you don't need to continue to do so for each specific condition. Unless, of course, you had intended to remove that first $gameover = 1.

Is it these kind of pairs that are bothering you?

LOSE -1 average dice roll: 7 LOSE -1 average dice roll: 8 LOSE -2 average dice roll: 7 LOSE -2 average dice roll: 7 LOSE -1 average dice roll: 7 LOSE -1 average dice roll: 7
because chances are high that these are just statistical fluctuations. If you look at the dice rolls that generate these losses you'll find that they're all different. The game of craps may just be weighted to encourage losses. ;)

You'll notice that 8 in my average dice roll. This is a running average and those 5 losses just happened to be results 5 - 10 of a simulation. The average ought to stay around 7 but will fluctuate around it through about 3 standard errors. ;)

So, in conclusion, yes there is a logic error in your code, your game occasionally ends without setting wins or losses. Had you kept a total of games played you might have spotted this. Whether or not your random number generator is non-random or your use of it is flawed can only really be determined by analysing it in the same using something similar to what dws suggested.

Your logic may contain other errors but as I've never studied the game of craps I have no idea what the frequency of certain results should be. Should you lose more often than you win, but win bigger? Should you lose 1 unit twice as often as you lose 2 units? I'm 100% certain that there are statistic tables out there for this game just as there are for other casino games. Once you have that data, you'll be able to analyse your results better and check whether they're in line with what they should be.

Hope it helps

jarich

PS: for the interests of simplifying your code, you can easily drop the database stuff.

PPS: FWIW in the interests of not installing more stuff, my roll function came out to be:

sub rolldice() { # was: my $roll = int(rand(12)) + 1; my $d1 = int(rand(6)) + 1; my $d2 = int(rand(6)) + 1; my $roll = $d1 + $d2; return $roll; }

Update: Didn't think hard enough about my rolldice function. Fixed now.


In reply to Re: check my logic & a random number issue by jarich
in thread 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.