Generating Random Numbers with known probability and more

Hi,
I am trying to solve this problem but can't figure out how to execute it. I know I need to create a line of code that allows me to pick either 0 or 1 to represent sweet and sour and needs to be simulated 500 times, but there is a predetermined probability for each one to be chosen. Can anyone give me some type of direction? Here is the question.
Suppose you have a box with candies. Note that 60% of the candies are sour, the rest are sweet. You extract 10 candies at random.
  • Code in MATLAB a simulation of the candy box and the extraction process.
  • Run 500 simulations of a. On each simulation, compute the number of sour candies you obtain.
  • Compute the theoretical value of the expected value of sour candies in b. and compare with the one you obtained.
  • Use the results of your simulation in b. to compute the variance.
  • Compute the theoretical value of the variance and compare with the obtained in d.

3 Comments

For homework or assignment questions, we expect efforts from students. If you are stuck, tell us the specific problem.
Here is what I have,
%either sweet or sour, 500 times
VAL = [0 1] % 2 values: 0=sour
P = .6 % probabbility of selecting sour candy
Ndraw = 500 % number of draws
R = rand(Ndraw,1) < P
SEL = VAL(2 - R)
sour = numel(find(R==0)) %answers # of sours
My question is that I don't think I have given the number of sours in EACH simulation with this code. I am trying to figure out how I can use matlab to calculate the number of sour candies in each simulation out of 500 simulations rather than the total number of sours after 500 simulations.
Remember:
You extract 10 candies at random.

Sign in to comment.

Answers (1)

Adam Danz
Adam Danz on 1 Dec 2019
Edited: Adam Danz on 2 Dec 2019
" I know I need to create a line of code that allows me to pick either 0 or 1 to represent sweet and sour ... but there is a predetermined probability for each one to be chosen. Can anyone give me some type of direction? "
If you have the statistics and machine learning toolbox, start my briefly reading the documentation and exploring the examples on this page (link below).

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Asked:

on 30 Nov 2019

Edited:

on 2 Dec 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!