Question

Can anyone please answer these? It's python coding question focusing on random practice!! 1. Write a...

Can anyone please answer these?

It's python coding question focusing on random practice!!

1. Write a function biasedCoinFlip(p) to return a biased coin flip that returns ''heads'' with probability p and tails otherwise. For example, when p=0.5, this function will behave like the last. On the other hand, p=0.25 will mean that there is only one chance in four of getting the result of "heads".

2. Write a function randomCard() to randomly return a card in a standard deck(suits: diamonds, hears,clubs,spades,values: Ace,2,3,4,5,6,7,8,9,10,Jack,Queen, King). For example, the function may return the string "Queen of hearts".

3. Write a function coinCount(n) that simulates n coin flips and prints the number of times "heads" and "tails" are flipped. For example, if the function is called with n=100, it may print out "47 heads, 53 tails".

4.Write a function dieCount(n) that simulates the roll of n dice rolls that prints the number of times each of the six values appears.

5.Write a function dieMaximum(n) that simulates the roll of n dice rolls and prints the highest number rolled.

6.Write a function sample(p) that samples one response from a population in which p% of the people answer "Yes" to a question and the rest reply "No"

7. Does your solution from the previous question work if p is not an integer(for example, 66.67)? How would you change your solution to make sure that it covers such cases?

8. Write a function estimateSample(p,n) that samples n people, by calling the function from the previous question, and prints how many people of these n people sampled reply "Yes" and "No"

0 0
Add a comment Improve this question Transcribed image text
Answer #1

# Please refer to the screenshots of the code to understand the indentation of code.

import random

def biasedCoinFlip(p):
return "Heads" if random.random() <= p else "Tails" # random.random() returns a float between 0 and 1

def randomCard():
A=["" for i in range(13)] # A contains values of cards
B=["" for j in range(4)] # B contains names of suits
  
A[0] = "Ace"
A[1] = "2"
A[2] = "3"
A[3] = "4"
A[4] = "5"
A[5] = "6"
A[6] = "7"
A[7] = "8"
A[8] = "9"
A[9] = "10"
A[10] = "Jack"
A[11] = "Queen"
A[12] = "King"

B[0] = "Spades"
B[1] = "Hearts"
B[2] = "Clubs"
B[3] = "Diamonds"

x = random.choice(A) # Chooses a random element in A, i.e. a card
y = random.choice(B) # Chooses a random suit in B, i.e. a suit
return (x + " of " + y) # returns the string containing both details of the card

def coinCount(n):
Count_heads = 0 # Creating counter of heads
Count_tails = 0 # Creating counter of tails
for i in range(n): # Number of iterations = Number of coin flips = n
p = random.randint(1,2) # value of p is either 1 or 2, 1 means heads and 2 means tails
if (p == 1):
Count_heads += 1 # Increases the counter of heads when heads is obtained
else:
Count_tails += 1 # Increases the counter of tails when tails is obtained
return (str(Count_heads) + " heads, " + str(Count_tails) + " tails") # returns the string containing both counts

def dieCount(n):
outs = [1,2,3,4,5,6] # Values on faces of the dice
counts = [0,0,0,0,0,0] # Counters of those values
for i in range(n): # Number of iterations = Number of dice rolls = n
x = random.choice(outs) # Chooses a random face of dice
counts[x-1] += 1 # Increases the counter of that random face by 1

a = "Number of times the values appear = "
for j in range(6):
a += str(outs[j])
a += ": "
a += str(counts[j])
a += " times, "

return a # returns the string containing details of counts of each value in dice

def dieMaximum(n):
outs = [1,2,3,4,5,6] # Values on faces of the dice
maxValue = 1 # Creating maximum value obtained so far, initialised to 1
for i in range(n): # Number of iterations = Number of dice rolls = n
x = random.choice(outs) # Chooses a random face of dice
if (x > maxValue): # if the value obtained is greater than the maximum value obtained so far, it updates the maxValue to currently obtained value
maxValue = x
return maxValue # returns the maximum value

def sample(p):
return "Yes" if random.random() <= p else "No" # random.random() returns a float between 0 and 1

def estimateSample(p,n):
Count_yes = 0 # Creating counter of yes reply
Count_no = 0 # Creating counter of no reply
for i in range(n): # Number of iterations = Number of people = n
answer = sample(p) # Stores the reply of the person in the current iteration
if (answer == "Yes"):
Count_yes += 1 # Increases the counter of yes if the person replied yes
else:
Count_no += 1 # Increases the counter of no if the person replied no
return (str(Count_yes) + " people replied yes, and " + str(Count_no) + " people replied no") # returns the string containing details of counts of replies of n people

Add a comment
Know the answer?
Add Answer to:
Can anyone please answer these? It's python coding question focusing on random practice!! 1. Write a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Practice: • function • loops • if condition • random Roll a dice many times, you...

    Practice: • function • loops • if condition • random Roll a dice many times, you will see that each face value will count of roughly 1/6 • write a function • roll a dice once, return value will be 1-6, • use your functin I call your function 6000 times • count how many times of face 1 • count how many times of face 2 • print out the numbers of total face 1, 2, ..., 6 you...

  • Random Number Generation and Static Methods Write a program that simulates the flipping of a coin...

    Random Number Generation and Static Methods Write a program that simulates the flipping of a coin n-times to see how often it comes up heads or tails. Ask the user to type in the number of flips (n) and print out the number of times the coin lands on head and tail. Use the method ‘random()’ from the Math class to generate the random numbers, and assume that a number less than 0.5 represents a tail, and a number bigger...

  • Write a function that flips a coin; it returns heads or tails. Use that function to...

    Write a function that flips a coin; it returns heads or tails. Use that function to write a GetHeadsInARow functions that accepts the number of heads to roll in a row before you return with total rolls it took to get that number of heads in a row. Please Code in Python3 my attempt below will not print out the answer and I do not know why def HeadTails(): from random import randrange """Will simulate the flip of a coin."""...

  • question 2 in C programming please PE-05b-01 (Six-sider) write a counter-controlled program that prompts the user...

    question 2 in C programming please PE-05b-01 (Six-sider) write a counter-controlled program that prompts the user to enter the number of times n to roll a six-sided die. The program should print to the screen the iteration of the loop and result for each roll 1 ) How many times would you like to roll? 3 roll 6 6 5 1 2) PE 05b 02 (Flash Cards) Write a counter-controlled program that creates addition problems for an elementary kid to...

  • How to write python code that is a dice rolling function that generates random numbers. The...

    How to write python code that is a dice rolling function that generates random numbers. The dice rolling function takes two arguments: the first argument is the number of sides on the dice and the second argument is the number of dice. The function returns the sum of the random dice rolls. For example, if I call roll dice(6,2) it might return 7, which is the sum of randomly chosen numbers 4 and 3. It somewhere along the lines of:...

  • 1. Roll an even dice and observe the number N on the uppermost face. Then toss...

    1. Roll an even dice and observe the number N on the uppermost face. Then toss a fair coin N times and observe X, the total number of heads that appear in N tosses. (i) Write down the conditional probability mass function pXIN 13) (ii) What is P(X )? (iii) What is E(X)?

  • 1. Roll an even dice and observe the number N on the uppermost face. Then toss...

    1. Roll an even dice and observe the number N on the uppermost face. Then toss a fair coin N times and observe X, the total number of heads that appear in N tosses. (i) Write down the conditional probability mass function pX|N(·|3). (ii) What is P(X = 5)? (iii) What is E(X)?

  • 1. Roll an even dice and observe the number N on the uppermost face. Then toss...

    1. Roll an even dice and observe the number N on the uppermost face. Then toss a fair coin N times and observe X, the total number of heads that appear in N tosses. (i) Write down the conditional probability mass function pxIN(-13). (ii) what is P(X = 5)? (ii) What is E(X)?

  • 1 [Run Lengths] Write a function (in Python and numpy) with the following specification def run_lenths(n,...

    1 [Run Lengths] Write a function (in Python and numpy) with the following specification def run_lenths(n, p): """Return a list of the run lengths in n tosses of a coin whose heads probability is p. Arguments: n--Number of tosses (a positive int), p--The probability of observing heads for any given toss (float between 0 and 1). """ For example, if the simulated sequence of coin tosses is HTTHHTHHHTTHHTTTTH, then the list of run lengths the function returns will be [1,...

  • This is Python coding question, and topic is recursive. Can anyone help me figuring this out?...

    This is Python coding question, and topic is recursive. Can anyone help me figuring this out? (Only recursive function is allowed.) Thanks. Write a function called Fibonacci(n) that takes as a parameter an integer n and returns the nth Fibonacci number. The first two Fibonacci numbers are 1 and every subsequent one is the sum of the previous two: 1, 1, 2, 3, 5, 8, 13, .... Write a recursive function to count the number of uppercase letters in a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT