vendredi 28 septembre 2018

Simulate an unfair die

So, I have to simulate the tossing of an unfair die in MATLAB, which has a 20% of probability to show each face between 1 and 4, and 10% of probability to show each face of 5 and 6. I have to generate 5000 random integers, representing the outcomes of tossing the die. I also have to print out the simulated expected value and standard deviation using 5000 repeated trials.

This is the code I wrote in MATLAB:

x=randi(6,1,5000); %Generate 5000 random values from 1-6

mean_x = mean(x);    %Find expected value
std_x = std(x);      %Find standard deviation

range_x = [1:6];

bar(range_x, pmf);    %plot the pmf

xlabel('Value of face')
ylabel('Simulated probability mass function')

fprintf('The expected value is %4.2f\n',mean_x);
fprintf('The standard deviation is %4.2f\n', std_x);


function pmf=finitepmf(sx,px,x) %function that finds pmf

    pmf=zeros(size(x(:))); 

    for i=1:length(x) 
        switch x(i)
            case 1 
                px = 0.2;
            case 2 
                px = 0.2;
            case 3 
                px = 0.2;
            case 4 
                px = 0.2;
            case 5 
                px = 0.1;
            case 6 
                px = 0.1;
        end
        pmf(i)= sum(px(find(sx==x(i)))); 
    end

end

However, I get the same pmf I would if the die was fair. What am I doing wrong?




Aucun commentaire:

Enregistrer un commentaire