Home > Software design >  How to simulate a win streak game min game in Python with non-random probabilities
How to simulate a win streak game min game in Python with non-random probabilities

Time:08-18

I am trying to simulate a mini win streak game with non-random probabilities. My goal is to answer: How many guesses (videos watched), on average, are needed to win the game?

Rules / Information:

  • The game is won after 3 consecutive "right" guesses
  • If the player makes a mistake, the win streak (counter) is reset to 0 and starts all over.
  • The probability of guessing right at every try is always 65%

I would like to get a list of 10 iterations of this game with the number of videos needed to win for each iteration

I would like the output of my code to be like: iteration : videos_watched

import random

outcome = [1,0]
trials = 10
streak_counter = 0
videos_watched = 0
lst = []

for i in range(trials):
    while streak_counter <3: ## player wins when hitting 3 consecutive right guesses
        trial_1 = (random.choices(outcome, weights= (65,35),k=1))[0] ## gives 1 or 0 with prob
        videos_watched = videos_watched   1 ## counter of iterations
        
        if trial_1 == 1: ## evaluates if the guess was successful
            streak_counter = streak_counter   1 ## streak position  1 if right
           
        else: ## evaluates if the guess was not successful
            streak_counter = 0 ## streak position to 0 if wrong
        
            
    print (i, videos_watched)

My problem is that I I get the following outcome: enter image description here

but I would like to get something like this: |i| videos_watched| |:---|:---| |0 | 3 | |1 | 5 | |2 | 8 | |3 | 7 | |4 | 5 | |5 | 6 | |6 | 4 | |7 | 3 | |8 | 8 | |9 | 5 |

CodePudding user response:

So, if a player makes 3 correct guesses within 10 trials, they win. This tells you how many wins there were in 100,000 games:

import random

trials = 10
watched = 0

def trial():
    global watched
    streak_counter = 0
    for i in range(trials):
        watched  = 1
        if random.random() < .65:
            streak_counter  = 1
            if streak_counter == 3:
                return True
        else:
            streak_counter = 0
    return False
               
rpts = 100000
wins = sum(trial() for _ in range(rpts))
print(wins, rpts, watched)

It appears that, on average, the player wins about 78.5% of the time, and they have to watch an average of 6.2 videos to get that.

Sample output:

78546 100000 625542
78596 100000 625818

CodePudding user response:

Found the solution

import random

outcome = [1,0,0,0] ## add as many elements as emojis available, 1 is always the right emoji
trials = 10000 ## number of model repetitions
total_videos = 0 ## video counter
streak_length = 5 ## number of win streak levels before reaching checkpoint

for i in range(trials):
    streak_counter = 0
    videos_watched = 0 
    
    while streak_counter < streak_length: ## player wins when hitting X consecutive right guesses
        trial_1 = (random.choices(outcome, weights= (45,18,18,18),k=1))[0] ### gives success / fail based on input weights; as many weights as outcomes
        videos_watched = videos_watched   1 ## counter of iterations
        
        if trial_1 == 1: ## evaluates if the guess was successful
            streak_counter = streak_counter   1 ## streak position  1 if right
           
        else: ## evaluates if the guess was not successful
            streak_counter = 0 ## streak position to 0 if wrong
        
    total_videos = total_videos   videos_watched
        
    ##print ("iteration", i, "reached with", videos_watched, "attempts")
    ##print(total_videos)

print ("Avg videos watched for win streak with", streak_length, "levels is", total_videos / trials )
  • Related