jeudi 30 avril 2015

Generating 2 random numbers with specific probabilities

I have earlier written a code that generates random number - 0 or 1 - that represents the outcome of a one toss, with the assumption that probability to have head or tail is 0.5 " equal probabilities".

Now I want to modify the code so that, it will represent a Bernolli trial. H - head- represents a success, and a variable p represents probability of success. I tried to search how to generate random number with specific probability however I didn't know exactly how it's done.

My Earlier Code

n = 1; %number of trials

% Generating a random number from 1 to n trials, logical condition <0.5
% indicates that function rand should generates only two outcomes,
% either 0 or 1.

% Let 1 indicates "H" and 0 indicates "T".
x = rand(1,n) <0.5;

% count how many H and T in each tossing trial
number_of_H =0 ; number_of_T  =0;

for n=1:n
    if(x(1,n)==1)
        number_of_H=number_of_H+1;
    end
    if(x(1,n)==0) 
        number_of_T = number_of_T+1;
    end
end

probability_H = number_of_H/n; 
probability_T = number_of_T/n; 

I have seen this r = random(pd) from mathwork reference but when I tried to replace 0.7 with pd as a probability it gives an error that this is not a probability distribution.




Aucun commentaire:

Enregistrer un commentaire