Write a MATLAB code that will generate a random experience, which can take only
ID: 3690060 • Letter: W
Question
Write a MATLAB code that will generate a random experience, which can take only two possible equally likely outcomes H (representing a heads outcome in a random coin toss) and T (representing a tails in a random coin toss) . Generate a sequence of toss outcomes of 10 random trials. Count how many times H and T are generated out of the 10 outcomes. How can you estimate the probability of H and T in this case? Repeat the experiment with a 100, 500 and 1000 outcomes generated. Estimate the probabilities in each case. What do you notice? Hints: Random integer numbers with equal probability are generated in MATLAB using the command function randi(). Read the help on MATLAB to know how to use the function by typing help randi in MATLAB command line.
Explanation / Answer
This is the problem for the coin toss simulation where we have a coin, which is tossed
and the occurence for the head and the tail is recorded.
Acording to the problem it is stated that the occurence of the events is equally likely,
which means that the ptobability of occurence of both the events are same.
So, we have assumed that if we recieve the outcome with probability of greater than 1, then
we will assign it as tail. Else we will assign it with head.
The code goes as follows:-
n=10; #no of trials
a=0 ; #no of occurence of head
b=0 ; #no of occurence of tail
y=NULL #initializing a vector of NULL values
for (i= 1:n) {
x=rand(1) #random uniform
if (x<0.5) { #if condition for assigning heads / tails
y[i]='H'
a=a+1;
} else {
y[i]='T'
b= b+1;
}
end
}
end
disp(y)
disp(a)
X = sprintf('No of the appearance of head is %d.',a);
disp(X)
Y= sprintf('No of the appearance of head is %d.',b);
disp(Y)
By using the random function we will generate the random numbers between zero and one and divide the occurence
accordingly.
Also each time when this program runs we wil be having different occurences of head and tail in the output.
We can estimate the probability of head and tail by couting the number of the head and tail, which we
implemented in the program.
Similarly, we can have the outcomes for 100,500 and 1000 number of inputs in this program. We just need to provide the
value of 'n' variable manually as per the requirement.
It is observed that mostly the probablity of the head is more than that of tail even the coin is equally biased.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.