Write a Matlab code that will generate a random number, which can take only two
ID: 3766958 • Letter: W
Question
Write a Matlab code that will generate a random number, which can take only two possible values 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, 200, 500 and 1000 outcomes generated. Estimate the probabilities in each case. What do you notice?
Hints: Random numbers with equal probability are generated in Matlab using the command function rand(). Read the help on Matlab to know how to use the function by typing help rand in Matlab command line.
Explanation / Answer
Probability of heads can be obtained by a simple formula: number of heads / total coin tosses.
For tails probability it is: number of tails / total coin tosses.
Here is the general code, which accepts the number of tosses, from the user, where you can enter any value, as you specified, in your problem, and it will generate, the number of heads, the number of tails, the heads probability, and tails probability as output.
heads = 0;
tails = 0;
n = input('Enter the number of trials: ');
for i = 1:n
x = randn();
if(x > 0)
toss = 'H';
heads = heads+1;
else
toss = 'T';
tails = tails+1;
end
end
display (heads);
display(tails);
headsProbability = heads/i;
tailsProbability = tails/i;
display(headsProbability);
display(tailsProbability);
After experimenting with all the values, you specified, and ofcourse, going beyond with values 10000, 100000, etc, it is observed that, the probability becomes, real close to (0.5) equality (thats how it should be), when the size is considerably large enough.
Just run the code with various values, and if you still have further queries, just get back to me.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.