Calculating experimental probability of head toss

0 votes

I want to calculate the experimental probability of heads in coin toss by generating 0s or 1s randomly and assign 'h' for 0 and 't' for 1 to n. The number of flips is 100.

import random

array_ht = []

flip_count = 0

while flip_count != 100:
    n = random.randint(0,1)
    if n == 0:
        n = "h"
    elif n == 1:
        n = "t"
    flip_count += 1
    array_ht.append(n)

for x in array_ht:
    h_count = 0
    if x == "h":
        h_count += 1

    print(h_count + "%")

The for loop looks through every object of array_ht and if it finds an "h", it adds 1 to the number of head flips. But the code isn't working and just prints "1% 0% 1% 0% 0% ..."

What should actually happen is for example if sequence generated was '0,1,1,0,0,0,1,1,1' then array_ht = [h,t,t,h,h,h,t,t,t] and probability to be printed is (4/9*100=) 44.444444%.

Actually we don't need to assign 'h' or 't' to n, we can just look for number of 0s but earlier I had wanted to print array_ht also.

Mar 22, 2022 in Machine Learning by Nandini
• 5,480 points
519 views

1 answer to this question.

0 votes

To answer your question. 
Refer to the code below

import random
toss = []
for j in range(100):
    toss.append(random.randint(0, 1))
for j in range(len(toss)):
    if toss[j] == 0:
        toss[j] = "h"
    else:
        toss[j] = "t" 

print("Probablity Heads:", str(round((toss.count('h')/len(toss))*100)) + "%")
print("Probablity Tails:", str(round((toss.count('t')/len(toss))*100)) + "%")
answered Mar 23, 2022 by Dev
• 6,000 points

Related Questions In Machine Learning

0 votes
0 answers
0 votes
1 answer

Plotting joint probability of two random variable choices

Create pandas.DataFrame , you can use seaborn.jointplot ...READ MORE

answered Mar 23, 2022 in Machine Learning by Nandini
• 5,480 points
1,546 views
0 votes
1 answer

Understanding the probability of a double-six if i roll two dice

The chance of not getting a double ...READ MORE

answered Mar 23, 2022 in Machine Learning by Nandini
• 5,480 points
20,380 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
0 votes
1 answer

Formula to calculate chance (probability) of a dice side based on its value

If I understand you correctly, you're looking ...READ MORE

answered Mar 17, 2022 in Machine Learning by Dev
• 6,000 points
769 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP