Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
vector pairs

Subject: vector pairs

From: Chris

Date: 8 Sep, 2010 04:05:04

Message: 1 of 5

I have a randomly generated vector of 5 numbers 1-6. I want to generate the numbers m times and keep track of how many times I have 3 of a kind. So far I have,

function [RMS,OD]= rolldice(m)

%--- Start vectors to keep track of number occurances

3k = [0]; %three of a kind

for i = 1:m % preform loop 1 through m times

die1 = ceil(6*rand(1)); %produces random number 1-6
die2 = ceil(6*rand(1));
die3 = ceil(6*rand(1));
die4 = ceil(6*rand(1));
die5 = ceil(6*rand(1));
results = [die1 die2 die3 die4 die5];
-------------------------------------------------------------------------
Do I need a switch statement now? I am confused on how to tell Matlab to look for pairs...

Subject: vector pairs

From: Darren Rowland

Date: 8 Sep, 2010 05:23:05

Message: 2 of 5

Try the function histc.
Also take a look at the help for the rand function to see that you can calculate all 5 random numbers at once.

Hth
Darren

Subject: vector pairs

From: Ross W

Date: 8 Sep, 2010 05:24:03

Message: 3 of 5

"Chris " <ridered300tr@hotmail.com> wrote in message <i6721g$pnf$1@fred.mathworks.com>...
> I have a randomly generated vector of 5 numbers 1-6. I want to generate the numbers m times and keep track of how many times I have 3 of a kind. So far I have,
>
> function [RMS,OD]= rolldice(m)
>
> %--- Start vectors to keep track of number occurances
>
> 3k = [0]; %three of a kind
>
> for i = 1:m % preform loop 1 through m times
>
> die1 = ceil(6*rand(1)); %produces random number 1-6
> die2 = ceil(6*rand(1));
> die3 = ceil(6*rand(1));
> die4 = ceil(6*rand(1));
> die5 = ceil(6*rand(1));
> results = [die1 die2 die3 die4 die5];
> -------------------------------------------------------------------------
> Do I need a switch statement now? I am confused on how to tell Matlab to look for pairs...

hi

%first count how many times each die value occurs
num_occ=hist(results,1:6);

%now look for 2 or 3 or whatever number of occurrences
npairs=sum(num_occ==2);
ntrips=sum(num_occ==3);

ross

Subject: vector pairs

From: Chris

Date: 8 Sep, 2010 05:51:06

Message: 4 of 5

Thanks for all the help!

Here is what I have now:

function [RMS,OD]= rolldice(m)

one_pair = [0];
two_pair = [0];
three_kind = [0];
full_house = [0];
five_alike = [0];

for i = 1:m
    dices = ceil(6*rand(5,1));
    disp(dices); % displaying dices
    count = hist(dices, [1,2,3,4,5,6]); % calculating counts
    disp(count); % displaying counts

two_pair = two_pair + sum(count==2)
three_kind = three_kind + sum(count==3) %three of a kind
four_kind = four_kind + sum(count==4) %four of a kind
five_alike = five_alike + sum(count==5) %five of a kind

end
end

My problem now is getting the 1 pair and 2 pair to display correctly. If I use:
two_pair = two_pair + sum(count==2) it will count 1 pair as a two pair. Should I use a loop to go through the vector and see if 2 appears more then one time?

Subject: vector pairs

From: Steven_Lord

Date: 8 Sep, 2010 13:37:22

Message: 5 of 5



"Chris " <ridered300tr@hotmail.com> wrote in message
news:i67889$6hl$1@fred.mathworks.com...
> Thanks for all the help!
>
> Here is what I have now:
>
> function [RMS,OD]= rolldice(m)
>
> one_pair = [0];
> two_pair = [0];
> three_kind = [0];
> full_house = [0];
> five_alike = [0];
>
> for i = 1:m
> dices = ceil(6*rand(5,1));
> disp(dices); % displaying dices
> count = hist(dices, [1,2,3,4,5,6]); % calculating counts
> disp(count); % displaying counts
>
> two_pair = two_pair + sum(count==2)

Incorrect. If count contains _one_ 2 then you have one pair. If it
contains _two_ 2's then you have two pair. And if it has _three_ 2's,
there's a bug somewhere :)

> three_kind = three_kind + sum(count==3) %three of a kind
> four_kind = four_kind + sum(count==4) %four of a kind
> five_alike = five_alike + sum(count==5) %five of a kind
>
> end
> end
>
> My problem now is getting the 1 pair and 2 pair to display correctly. If I
> use:
> two_pair = two_pair + sum(count==2) it will count 1 pair as a two pair.
> Should I use a loop to go through the vector and see if 2 appears more
> then one time?

numberOfPairs = sum(count==2);

Have fun implementing the rest of the Yahtzee scoring conditions.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us