Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

CS1201 Worksheet 10

Q 1) Two friends A and B play a game of tossing the coin. They take turns in tossing a fair coin (one which has
the same chance of landing Heads as Tails) repeatedly, starting with A. The one who gets the first Heads wins.
Simulate this game a large number of times and hence estimate the probability that A will win the game.
Repeat the problem, but this time with a biased coin - one which has a 40% chance of coming up Heads.
Hint : to simulate a random event which has a probability p of occurring, generate a random number using the
random() function. This number has the probability p of being less than p!
Q 2) Write a program that will solve the following problem :
What is the probability that if a fixed line segment is broken into three pieces (by breaking at two
randomly chosen points) the resulting pieces can form a triangle?
Remember that it is not in general true that three line segments can form a triangle - the largest piece must be
smaller than the sum of the other two.
Hint : Assume that the line segment is of length 1. Generate two random numbers to simulate the two breaks and
check whether the resulting pieces can form a triangle. Repeat a very large number of times and find the fraction of
times that you get a positive outcome.
Q 3) Write a program to estimate the probability that if three points are chosen at random inside a 1 L rectangle,
the resulting triangle will be obtuse. What estimates does your program yield for L = 1, 1.5 and 2?
Hint : You should write a function called isObtuse with the three sides as parameters which returns the Boolean
variables True or False according to whether the triangle is obtuse or note. Remember that for an obtuse angled
triangle, the three sides are related by c2 > a2 + b2 where c is (obviously) the largest side.
Q 4) The random() program in pythons random module that we used is supposed to generate uniformly distributed
random numbers between 0 to 1 - this means that all numbers in this interval are equally likely to turn up as you
repeatedly call the random() function. Let us try to see whether this is correct. To do this, we divide the interval
from 0 to 1 into a large number N of equal pieces, and keep track of the number of times a call to the random()
function returns a value in one of these pieces1 by means of the code below :
>>> from random import random
>>> N = 100
>>> freqs = [0 for i in range(N)]
>>> for i in range(10000):
...

x = random()

...

n = int(N*x)

...

freqs[n] += 1

>>> pdf = [fr/100.0 for fr in freqs]


What this code does is estimate the probability distribution function, p (x) defined so that the probability that a
random number will lie between x and x + x is (in the limit of infinitesimal x to be exact) p (x) x.
If you choose two numbers x and y at random between 0 and 1, then their product, z = xy, will of course be
between 0 and 1. However, if x and y are chosen with uniform probability density (so that both are equally likely
to lie anywhere in the interval (0, 1) ), z will not be uniformly distributed. It is quite easy to understand that z is
more likely to be on the smaller side. Write a program that will estimate the probability distribution function for
p (z) . Can you guess (or, better still, calculate) the analytic form for p (z)?

1 Each piece has a size of h = 1 . The ith piece stretches from ih to (i + 1) h (remember that the count starts from zero). If a
N
number x lies in this interval, then N x lies between i and i + 1 - so that int(N*x) will return i.

You might also like