Assignment Number 1
MATHEMATICAL METHODS III –
Probability and Statistics
Question 1:
Write your own code (any programming
language) for random number
generator. Verify your output of the code
if the sequence of numbers generated is
random or not.
Question 2:
Simulate Monty Hall problem and
validate your result with theoretical
result. (Any programming language)
CODE 1
REPORT
Question 1: Random Number Generator
1.1 Objective:
The goal of this task was to create a random number generator,
produce a sequence of random numbers, and determine if the
generated sequence shows randomness through statistical
analysis.
1.2 Code Explanation:
The random number generator uses the C standard library
function rand (), which is seeded with srand(time(0)) to
ensure each run produces a different sequence of
numbers.
It generates 100 random numbers, each ranging from 0 to
9.
The randomness of the sequence is checked using a Chi-
square test, which compares the observed frequencies of
each number with the expected frequencies. This
generates a Chi-square statistic to assess the deviation
from a uniform distribution.
The threshold for randomness is set at 15.507, derived
from a Chi-square distribution table for 9 degrees of
freedom (corresponding to 10 possible outcomes minus 1)
with a significance level of 0.05. If the calculated Chi-
square value is below this threshold, the sequence is
considered random.
1.3 Theoretical Background:
The Chi-square test is used to evaluate whether the
distribution of random numbers approximates the ideal
uniform distribution. In a truly random sequence, each
number (from 0 to 9) should appear about 10% of the time
when generating 100 numbers.
The Chi-square formula is:
Where:
ek is the observed frequency (how many times a number
appears).
fk is the expected frequency (which, in this case, is 10 for
each number since the range is 0-9 and we generate 100
numbers).
The Chi-square value quantifies how much the actual
distribution deviates from the expected uniform distribution. If
the statistic is below the threshold, we assume that the
sequence is random.
1.4 Code Output:
The program generates and prints a sequence of 100
random numbers between 0 and 9.
It then calculates the Chi-square statistic based on the
observed distribution of the numbers.
Finally, it prints whether the sequence can be considered
random based on the result of the Chi-square test.
1.5 Connection Between Theory and Output:
The theoretical basis for randomness is that a uniform
distribution of numbers will lead to a low Chi-square statistic,
indicating that the sequence behaves randomly.
CODE 2
REPORT
Question 2: Monty Hall Problem
2.1 Objective:
The objective of this part of the assignment was to simulate the
Monty Hall problem and validate the simulation results with the
theoretical outcome.
2.2 Code Explanation:
The Monty Hall problem simulates a game show where a
contestant chooses one of three doors, behind one of
which is a car, and the other two have goats.
After the contestant makes an initial choice, the host, who
knows what's behind the doors, opens one of the other
two doors, revealing a goat.
The contestant is then given the option to switch to the
remaining unopened door or stick with their original
choice.
The simulation runs 10,000 trials, counting the number of
times the contestant wins by switching and by staying.
2.3 Theoretical Background:
The Monty Hall problem is a classic probability puzzle. The
theoretical result shows that switching doors gives the
contestant a 2/3 chance of winning the car, while staying gives
only a 1/3 chance. This is because, when the contestant first
picks, there is a 1/3 chance they picked the car and a 2/3
chance they picked a goat. By switching after a goat is
revealed, the contestant effectively switches to the 2/3 chance
of winning.
2.4 Code Output:
The code simulates 10,000 trials of the Monty Hall
problem.
It calculates the percentage of times the contestant wins
by switching versus staying.
For 10,000 trials, the expected result is around 66.67%
wins by switching and 33.33% by staying.
2.5 Connection Between Theory and Output:
The simulation results align closely with the theoretical results:
Switching wins approximately 66.67% of the time.
Staying wins approximately 33.33% of the time. This
confirms that the simulation accurately reflects the
theoretical probability of the Monty Hall problem.
Conclusion:
Both parts of the assignment were completed successfully. The
random number generator was tested using a Chi-square test,
and the results confirmed the randomness of the sequence. The
Monty Hall problem was simulated, and the results validated
the theoretical expectation that switching gives a higher
probability of winning.
This structure explains both parts clearly and connects the
theory with the results from your code.