8 Replies - 1807 Views - Last Post: 14 March 2012 - 05:58 PM Rate Topic: -----

#1 Lenn0s  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-March 12

Locating Bug in Craps Simulation

Posted 14 March 2012 - 08:34 AM

I've have just finished working on a script to simulate multiple games of Craps and estimate the probability that the player wins.

Spoiler


However, according to the simulation, the player winning 100% of his games! I'm not quite sure why this is happening. Here is the piece of my script where all of the simulations are taking place:

def simulateGames(numOfGameSim):
    numOfWins = 0 # Set Win Count to 0
    
    # Simulate 'n' games
    for i in range(numOfGameSim):
        # Simulate dice role
        diceValue = random.randrange(2, 13)
        initValue = diceValue
        
        if initValue == 7 or 11:
            numOfWins += 1
        elif initValue == 2 or 3 or 12:
            numOfWins += 0
        else: # if game isn't decided in initial roll...
            while diceValue != initValue or 7:
                diceValue = random.randrange(2, 13)
            if diceValue == initValue:
                numOfWins += 1
            else:
                numOfWins += 0

    # return the win count and the winning probability    
    return numOfWins, (float(numOfWins) / float(numOfGameSim))



I've checked through this function a dozen times, but I can't seem to locate any flaws. Does anyone have any ideas about what could be happening?

Is This A Good Question/Topic? 0
  • +

Replies To: Locating Bug in Craps Simulation

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: Locating Bug in Craps Simulation

Posted 14 March 2012 - 09:24 AM

while diceValue != initValue or 7:
  diceValue = random.randrange(2, 13)


Doesn't this mean the program will loop indefinatley until the player rolls a 7 or their point? Code won't continue to line 17 until the player wins.

So yeah, the player always wins... eventually.

I think if you can put the python equivalent of a breakpoint at line 16 you will find this loops a LOT until the player wins, then continues to line 17.

This post has been edited by tlhIn`toq: 14 March 2012 - 09:24 AM

Was This Post Helpful? 0
  • +
  • -

#3 Lenn0s  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-March 12

Re: Locating Bug in Craps Simulation

Posted 14 March 2012 - 11:55 AM

View PosttlhIn`toq, on 14 March 2012 - 09:24 AM, said:

Doesn't this mean the program will loop indefinatley until the player rolls a 7 or their point? Code won't continue to line 17 until the player wins.


Thanks for the reply! :)

while diceValue != initValue or 7:
    diceValue = random.randrange(2, 13)



Yes, this loop does continue to re-roll the die indefinitely until it hits the initial roll or a '7', but rolling a '7' would mean that the player loses the game. After re-rolling, the following if statements would determine which value the loop stopped at.

if diceValue == initValue:
   numOfWins += 1 # WIN
else:
   numOfWins += 0 # LOSE



If the while loop stopped when it rolled the initial value, a win would be recorded. Otherwise, the win would not be recorded because the while loop stopped on a '7'.

I don't see how the player could always win if rolling a '7' during the while loop does not count as a win.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,518
  • Joined: 23-August 08

Re: Locating Bug in Craps Simulation

Posted 14 March 2012 - 12:03 PM

Try

while diceValue != initValue or diceValue == 7:

Was This Post Helpful? 1
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: Locating Bug in Craps Simulation

Posted 14 March 2012 - 12:03 PM

You need to throw the code into a debugger state so you can examine the value of the randomly generated number at this point.

I suspect that due to sheer speed of execution the new random is not actually a new number. Which would mean the second throw always matches the player's target point: Win!

I know its tedious, but that's what debugging is. YOu need to walk through the execution line-by-line and see WHY this is happening by comparing the actual values at run time.

Or put in console writeline with the generated random numbers. That way it will run, but report to you what is going on.
Was This Post Helpful? 1
  • +
  • -

#6 Lenn0s  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-March 12

Re: Locating Bug in Craps Simulation

Posted 14 March 2012 - 01:25 PM

View PostJackOfAllTrades, on 14 March 2012 - 12:03 PM, said:

Try

while diceValue != initValue or diceValue == 7:


Wouldn't that just make the while loop keep looping until the diceValue == initValue? Correct me if I'm wrong, but that would yield the same results as while diceValue != initValue:.

If '7' is rolled, the while loop would just continue to re-roll for the initial value.

View PosttlhIn`toq, on 14 March 2012 - 12:03 PM, said:

You need to throw the code into a debugger state so you can examine the value of the randomly generated number at this point.

I suspect that due to sheer speed of execution the new random is not actually a new number. Which would mean the second throw always matches the player's target point: Win!


Okay! I debugged my program with IDLE's debugger and some very bizarre things happened around here:

for i in range(numOfGameSim):
    diceValue = random.randrange(2, 13)
    initValue = diceValue
        
    if initValue == 7 or 11:
        numOfWins += 1



The program generated random numbers each run, but for some reason, executed these statements

if initValue == 7 or 11:
    numOfWins += 1



even if the condition was obviously false. After numOfWins += 1, the program went back to the top of the for loop and repeated the process for 'numOfGameSim' times adding 1 to numOfWins each time.

Spoiler


^^^ Full Source Code ^^^
Was This Post Helpful? 0
  • +
  • -

#7 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Locating Bug in Craps Simulation

Posted 14 March 2012 - 02:51 PM

I think what Jack was getting at is that:

while diceValue != initValue or 7:


Is probably not equating to what you think. It is saying if diceValue is not equal to initValue or "if the number 7 is false". Given that any number that isn't 0 is true, it will equate to:

while diceValue != initValue or True:


Which I'm guessing is why they win every time. Your statement:

if initValue == 7 or 11:
    numOfWins += 1



Will always be executed when it reaches it, and give the player a win, surely you mean:

if initValue == 7 or initValue == 11:
    numOfWins += 1


This post has been edited by Simown: 14 March 2012 - 06:09 PM

Was This Post Helpful? 2
  • +
  • -

#8 Lenn0s  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-March 12

Re: Locating Bug in Craps Simulation

Posted 14 March 2012 - 04:20 PM

View PostSimown, on 14 March 2012 - 02:51 PM, said:

I think what Jack was getting at is that:

while diceValue != initValue or 7:


Is probably not equating to what you think. It is saying if diceValue is not equal to initValue or "if the number 7 is false". Given that an number that isn't 0 is true, it will equate to:

while diceValue != initValue or True:


Which I'm guessing is why they win every time.


That makes sense! I revised all of the control structures in the script and it runs smoothly now. I appreciate everybody's help. =)

Anyway, the updated source code is:

Spoiler


The output also seems to agree with the craps winning probability (~49.3%):

Out of 1520 games, there were 745 wins. The probability of winning is 49.01%
Out of 1000 games, there were 468 wins. The probability of winning is 46.80%
Out of 500 games, there were 251 wins. The probability of winning is 50.20%


Was This Post Helpful? 0
  • +
  • -

#9 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Locating Bug in Craps Simulation

Posted 14 March 2012 - 05:58 PM

View PosttlhIn`toq, on 14 March 2012 - 03:03 PM, said:

I suspect that due to sheer speed of execution the new random is not actually a new number.


No.

The random number generator is initially seeded on import based on the system time (reference). Calling the random number generator multiple times in succession wouldn't give you the same value each time because it utilizes the current seed (which changes each time a random number is created).

This post has been edited by atraub: 14 March 2012 - 06:00 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1