Restart loop and add values in python? - python

The problem:
There are 3 paths where only one leads home.
The first path gets you lost for 3 days, then you're back to the beginning where you have to pick another path.
The second path gets you lost for 2 days, then you're back at the beginning and you have to pick another path.
The last door leads you home in 1 day.
Basically you keep going until you pick the last path. I'm trying to find the average time it takes to get home by simulating 1000 tries.
Here is what I have thus far:
days=0
for i in range(1000):
door=["a","b","c"]
numpy.random.choice(path)
if numpy.random.choice(path)=="a":
days=+2
if numpy.random.choice(path)=="b":
days=+3
if numpy.random.choice(path)=="c":
days=+1
print(steps)
As is, my code will just print out a value from 1-3 as the days.
I'm having trouble figuring out how to pick one and then accumulate it into the days and then restarting the loop until it picks path C.
I've done research and think a while loop might work but I don't know how to apply that.

You can use a while loop that keeps iterating while you are stuck and then when door 'a' is selected it adds the 1 to get home but then the person is no longer stuck so it drops out of the while loop. Then before entering the while loop again, just set stuck = True and the process continues always adding to the total number of days, then at the end just take the average.
import numpy
days=0
door=["a","b","c"]
N = 1000
for i in range(N):
stuck = True
while stuck:
if numpy.random.choice(door)=="a":
days += 2
if numpy.random.choice(door)=="b":
days += 3
if numpy.random.choice(door)=="c":
days += 1
stuck = False
print('Average number of days taken to get home: ', days / N)
I hope this helps!

Here's the code you're looking for:
import numpy
def runOnce():
days = 0
door=["a","b","c"]
while(True):
path = numpy.random.choice(door)
if path=="a":
days+=2
if path=="b":
days+=3
if path=="c":
days+=1
return days
total = 0
for i in range(1000):
total += runOnce()
print(total / 1000.0)

This code must solve your problem:
import random
doors = ['a', 'b', 'c']
total_days = 0
runs = 1000
for i in range(runs):
days = 0
choice = None
while choice != 'c':
choice = random.choice(doors)
if choice == 'a':
days += 2
if choice == 'b':
days += 3
if choice == 'c':
days += 1
total_days += days
avg_days = total_days / runs
print(avg_days)

I'm not quite sure on your rules, but this is my attempt
import numpy as np
def choose_path():
p =np.random.randint(3)
#print("Path = {}".format(p))
return p
N = 100000
days=0.0
for i in range(N):
#make sure you don't take the wrong path twice
taken = [False, False, False]
path = choose_path()
while(path != 2):
if(path==0):
if(not(taken[path])):
taken[path] = True
days += 2.0
if(path==1):
if(not(taken[path])):
taken[path] = True
days += 3.0
path = choose_path()
days += 1.0
# print("Days = {}".format(days))
print("Average for {} iterations = {}".format(N, days/N))
In contrast to the some of the other codes my guy doesn't take the same route twice. I'm not sure how you problem is defined. My solution seems to be 3.5.
Some of the mistakes you made are:
=+ is an assignment of a positive number a = +3 or a = -3
+= is an increment a = a + 3 <=> a += 3
you define door, but never use it
you never define steps but you use it
I think you should come up with an algorithm first and then implement it.

There are a few problems with your code. For example, you define a door list of possible choices, but then you pass path to the choice function. At the end of your program you print steps, but that's not defined anywhere. Instead, you should be printing days, or days / 1000. You need to pay attention to things like that when you're programming!
As others have shown, you need to do this with two loops. Each iteration of the outer loop performs a trial. The inner loop chooses paths until you get home and adds the day counts to the current total.
In your code, each if test generates a fresh random choice on top of the one you make at the start of the loop. That's not right. Just make the choice at the top of the loop, determine how many days to add to the count, and if you're home, break out of the loop.
We can do this in a simpler way. Rather than choosing from 'a', 'b', or 'c', just choose from 1, 2, or 3, the number of days each path takes. And as I said earlier, there's no need to use Numpy for this, we can just call the random module functions directly instead of letting Numpy do it on our behalf.
Here's a short demo.
from random import randint
trials = 10000
days = 0
for n in range(trials):
while True:
path = randint(1, 3)
days += path
if path == 1:
break
print(days, days / trials)
typical output
59996 5.9996
We can get a more accurate estimate to the true expected time by performing multiple runs and averaging the results. We could do that by wrapping the previous code in an extra loop, but it makes the code more readable if instead we wrap the old code in a function, and call that function in a loop. Like this:
from random import randint
def sim(trials):
days = 0
for n in range(trials):
while True:
path = randint(1, 3)
days += path
if path == 1:
break
return days
num = 10
trials = 10000
total = 0
for i in range(num):
days = sim(trials)
x = days / trials
print(i, x)
total += x
print('Final', total / num)
typical output
0 5.9732
1 6.007
2 6.0555
3 5.9943
4 5.9964
5 5.9514
6 6.0689
7 6.0457
8 5.9859
9 5.9685
Final 6.00468
It looks like the true expected value is 6 days. Actually, it's not hard to show that mathematically.
Let d equal the expected number of days to get home. 1/3 of the time we get home in 1 day, 1/3 of the time we get back to the start in 2 days and so we still have d days before we get home, and 1/3 of the time we get back to the start in 3 days and so, once again we still have d days before we get home.
We can put that into an equation:
d = (1/3)*1 + (1/3)*(2 + d) + (1/3)*(3 + d)
3*d = 1 + 2 + d + 3 + d
3*d = 6 + 2*d
d = 6

Related

Average number of streak of heads or tails in sequence of coin tossing

Below python code represents a small 'game':
I flip a coin 100 times and get a sequence of Heads and Tails
I try to figure out how many times in that sequence it happens that there are 6 Heads or 6 Tails after each other.
I want to run this 10000 times and then calculate the average occurrence of streaks per sequence.
I know the solution is that there are about 0.8 streaks per sequence, but I get to a number of 1.6 and I cannot figure out what I am doing wrong...
I have obviously seen other solutions, but I would like to figure out how I can make this specific code work.
Could you have a look at the below code and let me know what I am doing wrong?
import random
numberOfStreaks = 0
possib = ['H', 'T']
folge = ''
x = 0
while x < 10000:
for i in range (100):
folge = folge + str(random.choice(possib))
numberOfStreaks = folge.count('TTTTTT') + folge.count('HHHHHH')
x = x + 1
print(numberOfStreaks)
Since you "know" the answer you seek is ~= 0.8:
I believe you have misinterpreted the question. I suspect that the question you really want to answer is the (in)famous one from "Automate the Boring Stuff with Python" by Al Sweigart (emphasis mine):
If you flip a coin 100 times ...
... Write a program to find out how often a streak of six heads or a
streak of six tails comes up in a randomly generated list of heads and
tails. Your program breaks up the experiment into two parts: the first
part generates a list of randomly selected 'heads' and 'tails' values,
and the second part checks if there is a streak in it. Put all of this
code in a loop that repeats the experiment 10,000 times so we can find
out what percentage of the coin flips (experiments) contains a
streak of six heads or tails in a row.
Part 1 (generate a list of randomly selected 'heads' and 'tails' values):
observations = "".join(random.choice("HT") for _ in range(100))
Part 2 (checks if there is a streak in it.):
has_streak = observations.find("H"*6) != -1 or observations.find("T"*6) != -1
Part Do Loop (put code in a loop that repeats the experiment 10,000 times):
experimental_results = []
for _ in range(10_000):
observations = "".join(random.choice("HT") for _ in range(100))
has_streak = observations.find("H"*6) != -1 or observations.find("T"*6) != -1
experimental_results.append(has_streak)
Part Get Result (find percentage of the experiments that contain a streak):
print(sum(experimental_results)/len(experimental_results))
This should give you something close to:
0.8
Full Code:
import random
experimental_results = []
for _ in range(10_000):
observations = "".join(random.choice("HT") for _ in range(100))
has_streak = observations.find("H"*6) != -1 or observations.find("T"*6) != -1
experimental_results.append(has_streak)
print(sum(experimental_results)/len(experimental_results))
If however, the question you seek to answer is:
On average, how many occurrences of of at least 6 consecutive
heads or tails there are in 100 flips of a coin?
Then we can count them up and average that like:
import random
def count_streaks(observations):
streaks = 0
streak_length = 1
prior = observations[0]
for current in observations[1:]:
if prior == current:
streak_length += 1
if streak_length == 6:
streaks += 1
else:
streak_length = 1
prior = current
return streaks
experimental_results = []
for _ in range(10_000):
observations = [random.choice("HT") for _ in range(100)]
observed_streaks = count_streaks(observations)
experimental_results.append(observed_streaks)
print(sum(experimental_results)/len(experimental_results))
This will give you a result of about:
1.50
Note:
Your code uses folge.count('TTTTTT'). I believe this code and any answer that uses a similar strategy is likely (over the course of 10k experiments) to overestimate the answer as ("H"*12).count("H"*6) is 2 not 1.
For example:
This otherwise excellent answer by #samwise (Probability of streak of heads or tails in sequence of coin tossing) consistently generates results in the range of:
1.52
You're appending to folge each time through the x loop, so the 10000 different runs aren't independent of one another -- you don't have 10000 different sets of 100 tosses, you have a single set of 1000000 tosses (which is going to have slightly more streaks in it since you aren't "breaking" it after 100 tosses).
What you want to do is count the streaks for each set of 100 tosses, and then take the mean of all those counts:
from random import choice
from statistics import mean
def count_streaks(folge: str) -> int:
return folge.count("TTTTTT") + folge.count("HHHHHH")
print(mean(
count_streaks(''.join(
choice("HT") for _ in range(100)
))
for _ in range(10000)
))

Comparing multiple coin flip experiments. 1 Experiment = 100 Flips. Trying to simulate 10 experiments

import random as rd
n = 0
ListOfStreaks = []
ListOfResults = []
while n != 10:
numberOfStreaks = 0
for i in range(100):
Flip = rd.randint(0,1)
ListOfResults.append(Flip)
for i in range(96):
count = 0
for j in range(6):
if ListOfResults[i] == ListOfResults[i + j]:
count += 1
if count == 6:
numberOfStreaks += 1
count = 0
else:
continue
else:
break
ListOfStreaks.append(numberOfStreaks)
n += 1
print(ListOfStreaks)
print(len(ListOfResults))
In the code above, I am able to successfully flip a coin 100 times, and examine how many times in the 100 flips Heads or Tails came up six time in a row. I am unable to properly set up the code to run the experiment 10 times in order to examine how many times Heads or Tails came up six times in a row in each of the single experiments. The goal is to not flip the coins 1,000 times in a row but 10 experiments of flipping 100 coins in a row.
The exercise focuses on later being able to simulate the experiment 10,000 times in order to see what the probability is of Heads or Tails appearing six times in a row in 100 flips. Essentially, I am trying to gather enough of a sample size. While there are actual statistical/probability methods to get the exact answer, that isn't what I am trying to focus on.
CoinFlip Code
Your key problem appears to be that you have ListOfResults = [] outside of your while loop, so each run adds another 100 entries to the list instead of setting up a new test.
I've replaced the initial for loop with a list comprehension which sets up a new sample each time.
import random as rd
list_of_streaks = []
for _ in range(10):
list_of_results = [rd.randint(0,1) for _ in range(100)]
number_of_streaks = 0
for i in range(96):
if sum(list_of_results[i: i+6]) in(0, 6):
number_of_streaks += 1
list_of_streaks.append(number_of_streaks)
print(list_of_streaks)
print(len(list_of_results))
You also don't need the inner for loop to add up all of the 6 flips - you can just sum them to see if the sum is 6 or 0. You appear to have just tested for heads - I tested for 6 identical flips, either heads or tails, but you can adjust that easily enough.
It's also much easier to use a for loop with a range, rather than while with a counter if you are iterating over a set number of iterations.
The first comment from #JonSG is also worth noting. If you had set up the individual test as a function, you'd have been forced to have ListOfResults = [] inside the function, so you would have got a new sample of 100 results each time. Something like:
import random as rd
def run_test():
list_of_results = [rd.randint(0,1) for _ in range(100)]
number_of_streaks = 0
for i in range(96):
if sum(list_of_results[i: i+6]) in(0, 6):
number_of_streaks += 1
return number_of_streaks
print([run_test() for _ in range(10)])
print(len(list_of_results))

Count the dice throws to get to file 5 100 times(board is 0-5)

I'm trying to find out how many times you have to throw the dice to get on file 5 100 times(board is played from 0 to 5). This is how I tried(I know the answer is 690 but I don't know what I'm doing wrong).
from random import *
seed(8)
five = 0
count = 0
add = 0
while five < 100:
count = count + 1
print(randint(1,6))
add = add + randint(1,6)
if add % 5 == 0 :
five = five + 1
else: add = add + randint(1,6)
print(count)
This is the code I think you were trying to write. This does average about 600. Is it possible your "answer" came from Python 2? The random seed algorithm is quite likely different.
from random import *
seed(8)
five = 0
count = 0
add = 0
while five < 100:
count += 1
r = randint(0,5)
if r == 5:
five += 1
else:
add += r
print(count, add)
You're adding a second dice throw every time you don't get on 5, this makes the probability distribution irregular (i.e. advancing by 7 will be more probable (1/6) than any other value, e.g. 1/9 for 5) so your result will not be the same as counting single throws.
BTW there is no fixed result for this, just a higher probability around a given number of throws. However, given that you seeded the random number generator with a constant, every run should give the same result. And it should be the right one if you don't double throw the dice.
Here is an example of the process that arrives at 690:
import random
random.seed(8)
fiveCount = 0
throwCount = 0
position = 0
while fiveCount < 100:
position = (position + random.randint(1,6)) % 6
throwCount += 1
fiveCount += position == 5
print(throwCount) # 690
Other observations:
Updating the position wraps around using modulo 6 (there are 6 positions from 0 to 5 inclusively)
Your check of add%5 == 0 does not reflect this. It should have been add%6 == 5 instead but it is always preferable to model the computation as close as possible to the real world process (so keep the position in the 0...5 range)

How can I average multiple outputs independently in python

PYTHON 3: Hi, so I have this piece of code
for money in range(0, 2501, 500):
print("{} Euro".format(money), end='')
throws = 0
d = trump.possession(board)
while False in d.values():
prevLoc = piece.location
piece.move(trump.throw())
throws += 1
if piece.location < prevLoc:
money += 200
if board.names[piece.location] in d and d[board.names[piece.location]] == False and money >= board.values[piece.location]:
money -= board.values[piece.location]
d[board.names[piece.location]] = True
return throws
and this code takes 0 money to start with, runs the code, looks for amount of throws required to buy the entire board, does the same with 500 starting money, 1000 and so forth
my question is, how can i take the average of the throws to buy the entire board for each starting value? the way my code is now it returns the amount of throws for all the starting values, but simulated once, so it may not be accurate.
I searched a lot, and tried some things but i had problems with this one because I want to like run it, say for example, 2000 times, and get the average for each starting value for the money.
anyone got any tips for this? been struggling on it for a while..
i tried making a for loop from 0 to 2000 and then inside of that another for loop that prints 0-2500 and then uses the code below in a function, appends the return value of throws into a list and sums it up and devides it by 2000, it did not turn out so good...
I'm going to assume this is in a function, due to the return statement. You need to collect outputs into a list and then average that at the end.
def calc_throws(simulations):
throw_list = []
average = lambda x: sum(x)/len(x)
for i, money in enumerate(range(0, 2501, 500)):
print("{} Euro".format(money), end='')
throw_list.append([money, []])
for _ in range(simulations):
throws = 0
d = trump.possession(board)
while False in d.values():
prevLoc = piece.location
piece.move(trump.throw())
throws += 1
if piece.location < prevLoc:
money += 200
if board.names[piece.location] in d and d[board.names[piece.location]] == False and money >= board.values[piece.location]:
money -= board.values[piece.location]
d[board.names[piece.location]] = True
throw_list[i][1].append(throws)
throw_list[i][1] = average(throw_list[i][1])
return throw_list
Rather than a single number, this returns a list of lists like #[[0,20],[500,15],...[2500,3]] (or whatever reasonable numbers are) which gives you the average for each amount of starting money.

running a program over x amount of times

I'm trying to write a function that calls a function (roll die() which rolls a die 1000 times and counts on a list [1,2,3,4,5,6] so an outcome might be [100,200,100,300,200,100]) and tells it to run it x amount of times. It seems my code is printing it over and over again x times
#simulate rolling a six-sided die multiple tiems, and tabulate the results using a list
import random #import from the library random so you can generate a random int
def rollDie():
#have 6 variables and set the counter that equals 0
one = 0
two = 0
three = 0
four = 0
five = 0
six = 0
#use a for loop to code how many times you want it to run
for i in range(0,1000):
#generate a random integer between 1 and 6
flip = int(random.randint(1,6))
# the flip variable is the the number you rolled each time
#Every number has its own counter
#if the flip is equal to the corresponding number, add one
if flip == 1:
one = one + 1
elif flip == 2:
two = two + 1
elif flip == 3:
three = three + 1
elif flip == 4:
four = four + 1
elif flip == 5:
five = five + 1
elif flip == 6:
six = six + 1
#return the new variables as a list
return [one,two,three,four,five,six]
the new function that I am having problems with is:
def simulateRolls(value):
multipleGames = rollDie() * value
return multipleGames
I would like to see a result like this if you typed in 4 for value
[100,300,200,100,100,200]
[200,300,200,100,100,100]
[100,100,100,300,200,200]
[100,100,200,300,200,100]
Can someone guide me in the right direction?
You can get what you want like this:
def simulateRolls(value):
multipleGames = [rollDie() for _ in range(value)]
return multipleGames
By the way, your original function seems to work perfectly fine, but if you're interested, you can remove some redundancy like this:
def rollDie():
#have 6 variables and set the counter that equals 0
results = [0] * 6
#use a for loop to code how many times you want it to run
for i in range(0,1000):
#generate a random integer between 1 and 6
flip = int(random.randint(1,6))
# the flip variable is the the number you rolled each time
results[flip - 1] += 1
return results
The line
multipleGames = rollDie() * value
will evaluate rollDie() once and multiply the result by value.
To instead repeat the call value times do this.
return [rollDie() for i in xrange(value)]
You can also simplify your rollDie function by working with a list throughout
import random #import from the library random so you can generate a random int
def rollDie():
result = [0] * 6
for i in range(0,1000):
result[random.randint(0,5)] += 1
return result

Categories

Resources