1

Last night I was thinking to my self about the probability of getting the same outcome in "Rock, Paper, Scissors" 10 times in a row. I worked out how to do that and completed that task but then I wanted to challenge myself a bit, so I wanted to adapt the program so it ran the initial program a number of times (10,000) and then outputted the average result, which I hoped would be close to the probability of getting the same 10 times in a row. Please note that I am not taking into account tactics, just that both players randomly pick either r, p, or s.

def rps():

   num=0 # num is the number of times i want the programme to run while roll<10: 
   total=0 # this should be close to 3^10 * 10000, its the result of all the tries added together 

   while num <10001:
      tries=0 # this is how many times the programme has tried to get p1=p2
      roll=0 # this is the amount of times it has counted p1=p2 (it gets re-set everytime it reaches 10)
      import random
      while roll <10:
         p1=random.randint(1,3)
         p2=random.randint(1,3)
         if p1==p2:
            roll=roll+1
         else:
            tries=tries + 1
            roll=0    
            num=num+1
         if roll==10:
            total=total+roll
            roll=0
      if num==10000:
         print (num)
         print (total/num)
rps()
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
Joe
  • 21
  • 2
  • Why the downvotes? Joe has explained what he wants to calculate, and has posted some relevant code. Sure, the logic of that code doesn't quite do what he wants, but that's not a reason to downvote the question. – PM 2Ring May 26 '18 at 08:47
  • I don't understand why @Joe got all those downvotes! – O_o May 26 '18 at 08:51
  • BTW, `import` statements should be at the start of the script. They definitely should not be placed inside loops. It won't make the program malfunction, since Python doesn't re-import a module that it's already loaded, but it still has to check if it's loaded every time it hits the `import` inside the loop, which is inefficient. – PM 2Ring May 26 '18 at 08:52
  • In case it's not clear, by "same outcome", Joe want to find the odds of 10 "tie" rounds in a row. – PM 2Ring May 26 '18 at 09:07

2 Answers2

1

There are quite a few problems with the program, for once, there isn't any use for the second for loop, I get that you are using the second for loop to reset the counter if the number of consecutive rolls reaches 10, but you are already doing that here

if roll==10:
    total=total+roll
    roll=0

by setting roll=0, you are resetting it. also, the last if condition adds to the complexity,

if num==10000:
         print (num)
         print (total/num)

instead of this, you can just print the average outside the loop like this

if roll==10:
    total=total+roll
    roll=0
print (total/10000) #this being outside the while loop

one more thing, you are incrementing num only when roll1 != roll2, this adds to the number of times the loop has to run This is how the program came out after the changes

import random
def rps():
    num=0 #num is the number of times i want the programme to run while roll<10:
    total=0 #this should be close to 3^10 * 10000, its the result of all the tries added together
    roll = 0
    while num <10001:
        tries=0 #this is how many times the programme has tried to get p1=p2
        p1=random.randint(1,3)
        p2=random.randint(1,3)
        if p1==p2:
            roll = roll+1
        else:
            tries = tries + 1
            roll = 0
        if roll==10:
            print(roll)
            total += roll
            roll=0
        num = num + 1
    print (total/10000)
    print(tries)
rps()

The answer coming out was 0,I guess it was highly unlikely that you get 10 in a row.

loksoni
  • 177
  • 3
  • 12
  • im not sure thats quite right for some reason when you print total it = 0 whereas it should equal around 10000 * 3^10 – Joe May 26 '18 at 09:49
  • I don't think I am fully understanding what you are trying to say, do you mean to say that total is the total number of times the program played the game? because in the program, total is the total number of times both players got the same output for 10 consecutive terms. – loksoni May 26 '18 at 10:06
0

Some self-explanatory code (so you can also learn to write clean code):

import random

def get_10_outcomes():
    return [
        (random.randint(1, 3), random.randint(1, 3))
    ]

def have_we_got_10_in_a_row():
    return (
        len(set(get_10_outcomes()))
        == 1
    )

def how_many_10_in_a_row_in_10000():
    return len(list(filter(
        None,
        [have_we_got_10_in_a_row() for _ in range(10000)]
    )))

def get_chance_of_10_in_a_row():
    return how_many_10_in_a_row_in_10000() / 10000

chance = get_chance_of_10_in_a_row()
print('{:.3%}'.format(chance))
Bob
  • 5,809
  • 5
  • 36
  • 53
  • thank you but i wanted advice on how to change my code not a completely new one – Joe May 26 '18 at 09:36