Pregunta

I want to interate 1000 times over the following function to find out if you win or loose money in this game.

The game is designed as such that you throw a pair of dice and get money back or loose money. Let's say we start with 5 coins.

Throwing a 12 yields 1.5 coins.

Throwing an 11 yields 1 coins.

Throwing a 10 yields 0.5 coins.

Throwing a 9,8 or 7 yields nothing.

Throwing a 6,5,4,3,2 or 1 deducts 0.5 coins from your amount of coins.

This is what my implementation looks like so far:

def luckCalc():

    amount = 5

    # if 12 then 1/36 chance

    if random.randrange(1,7) == 6 and random.randrange(1,7) == 6:
        amount = amount + 1.5

    # if 11 then 2/36 chance
    elif  (random.randrange(1,7) == 5 and random.randrange(1,7) == 6) or (random.randrange(1,7) == 6 and random.randrange(1,7) == 5):
        amount = amount + 1

   # if 10 then 3/36 chance

   elif (random.randrange(1,7) == 5 and random.randrange(1,7) == 5) or (random.randrange(1,7) == 4 and random.randrange(1,7) == 6) or (random.randrange(1,7) == 6 and random.randrange(1,7) == 4):
       amount = amount + 0.5

   # if 9,8,7
   # 4/36 + 5/36 + 6/36 chance
   # 1+6, 2+5, 3+4, 4+3, 5+2, 6+1 chance
   # 2+6, 3+5, 4+4, 5+3, 6+2 chance
   # 3+6, 4+5, 5+4, 6+3 chance
   # then no change in amount 

   # if 6,5,4,3,2,1
   # chances...
   # then amount -0.5

return amount 

# Iterate over the dice throwing simulator and calculate total

total = 0.0
for a in range(1000):
    total = total + luckCalc()

print (total)

I stopped coding towards the end of the function, because I recognised that there must be a more elegant solution on how to achieve this. Any interesting suggestions, what is this Monte Carlo I keep hearing about?

¿Fue útil?

Solución

Each time you call random.randrange(1,7), you generate a new random number. Since you're testing a single "turn", roll twice:

def roll_die():
    return random.randrange(1, 7)

total = roll_die() + roll_die()

And see if the sum is in a range:

def play_turn():
    total = roll_die() + roll_die()

    if total == 12:
        return 1.5
    elif total == 11:
        return 1.0
    elif total == 10:
        return 0.5
    elif total <= 6:
        return -0.5
    else:  # total is 7, 8, or 9
        return 0

Here's the result of 100,000 rounds:

>>> from collections import Counter
>>> counts = Counter(play_turn() for i in xrange(100000))
>>> counts
    Counter({-0.5: 41823, 0: 41545, 0.5: 8361, 1.0: 5521, 1.5: 2750})
>>> probabilities = {score: count / 100000.0 for score, count in counts.items()}
>>> probabilities
    {-0.5: 0.41823, 0: 0.41545, 0.5: 0.08361, 1.0: 0.05521, 1.5: 0.0275}

Otros consejos

You can actually roll (ha!) everything you are doing into a single function:

from random import randrange

def play_game(rolls=1000, amount=5, n=6):
    """Play game 'rolls' times, starting with 'amount' on 'n'-sided dice.""" 
    for i in range(rolls):
        roll = randrange(1, n+1) + randrange(1, n+1)
        if roll == 12:
            amount += 1.5
        elif roll == 11:
            amount += 1
        elif roll == 10:
            amount += 0.5
        elif roll < 7:
            amount -= 0.5
    return amount

I notice a few things in your code. First, for the 6-1 cases you're not actually subtracting 0.5 from the amount. Second, since you don't pass in the initial amount each loop you're adding between 5 and 6.5 to your total, which makes the total pretty pointless.

A more effective total would be to pass in the amount each time:

def luckCalc( amount ):

And then for your loop:

total = 5.0
for a in range(1000):
    total = luckCalc(total)

Blender's answer, which just posted as I was writing this, is a great way to simplify your main function.

I personally like setting up my results table as an array (or a dictionary, but this suited my purpose better since every result was one of a small number of possible integers), with the index of each dice roll set to the value of the resulting change. See below.

import random

def luckCalc(coins=5):

  diceroll = random.randint(1,6)+random.randint(1,6) #roll them bones

  #here's that table I was talking about....
  results_table = ['index 0 is blank',"you can't roll a one on two dice",-.5,-.5,-.5,-.5,-.5,0,0,0,.5,1,1.5]

  coins += results_table[diceroll] #changes your coins value based on your roll (as an index of results_table)

  if results_table[diceroll] > 0: #change the string if your result was + or -
    result = "gained {}".format(results_table[diceroll])
  else:
    result = "lost {}".format(results_table[diceroll]*-1)

  print("You {} coins, putting you at {}".format(result,coins)) #report back to the user
  return coins #this is how you save your output


#CONSTANTS GO HERE -- YOU CAN CHANGE THESE TO CHANGE YOUR PROGRAM
STARTING_COINS = 5
HOW_MANY_ITERATIONS = 1000

#this way we don't modify a constant
coins = STARTING_COINS

#do it how many times?
for _ in range(HOW_MANY_ITERATIONS): #oh yeah that many times
  coins = luckCalc(coins) #runs the function and saves the result back to coins

#report to the user your final result.
print("After {} rolls, your final total is {}".format(HOW_MANY_ITERATIONS,coins))
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top