# Simple help with matlab-dice game needed

1. Sep 19, 2006

### Thomas98

I'm making a simple dice-game with matlab and I
want to extend the functions of it. What I have now is
that I throw 5 dices (each dice gives a value of 1 to
6), and I make matlab display the value of each dice,
e.g [1 3 5 2 1] and count how many of each value are
represented e.g in my case we have 2 ones, 1 twos, 1
threes, 0 fours, 1 fives, 0 sixes, that outputs the
vector [2 1 1 0 1 0]. The script-code used for the
above function is:

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

OK, here are some functions I want add to the program:
#1) I want display through one digit the value that is
most represented. e.g from the dice-throw [4 5 4 4 1]
the output should be 4.
#2) According to the game strategy you should save the
identical dices and throw the rest over again.OK,
that means that I need code that outputs, in my case
above, the vector [2 5], i.e dice number 2 and 5. If
we have a more complex situation [4 4 2 2 5], then we
an output: [1 2 5] or [3 4 5]; or according to an
other fashion: [* * 2 2 *] or [4 4 * * *].

#3) OK, I want to repeat this procedure until all
dices are of the same value, e.g all 4:s. And I want
as a final output the number of throws that were
needed to get five dices of the same value.

/Thomas

2. Sep 19, 2006

### CRGreathouse

0. "Die" is the singular of "dice". One die, two dice.

1.
Code (Text):
dice = ceil(6*rand(5,1));
disp(dice); % displaying dice
count = hist(dice, [1,2,3,4,5,6]); % calculating counts
disp(count); % displaying counts
disp(mode(dice)); % "most represented"
2. Use the mode (above) and filter out all numbers not equal to it.

3. Put it all in a loop with a variable counter, which you can output at the end.

3. Sep 20, 2006

### Thomas98

Hi, I ran the code you gave me but it seems like the mode-function is abscent with my version, I ran 'help mode' in the prompt but it replies 'mode.m not found' . I have tried both Matlab version 6 and 7. Maybe I need to download extention packs for matlab or so?

4. Sep 20, 2006

### Thomas98

Hi again, I solved the problem with the mode-function. Regarding the loop part, how is the while-statement suppose to look? I have a counter k=1 and I have a condition dice~=mode(dice) but how are these supposed to fit together , I wrote:
while dice.*(dice~=mode(dice))~=[0,0,0,0,0]
??
end
Can you give a couple of pointers?

5. Sep 20, 2006

### CRGreathouse

First of all, I don't have a copy of Matlab handy, so I'm not able to test my own code. Mode does show up in the documentation, though, so I don't know what the problem was.

For the loop in #3, you need to go through each element of the list and check if it is equal to the mode. If not, you need to remove the element from the list.

6. Sep 22, 2006

### Thomas98

Hi, thanks for the tip, this is what I ended up with:

dice = ceil(6*rand(5,1));
disp(dice);
count = hist(dice, [1,2,3,4,5,6]);
disp(count);
disp(mode(dice));
A=(dice.*(dice~=mode(dice)));
disp(A);
k=1;
while any(dice.*(dice~=mode(dice)))~=0
dice(find(A))=ceil(6*rand(size(find(A))));
k=k+1;
end
disp(k);

Ok, I want to extend this with a monte carlo-type simulation #1) First I want a function with an input of the amount of series of five identical dice,e.g '3' would represent how many throws are needed to get 3 series of five identical dice. #2) The second input I need is the amount of experiments of the above. And I want to plot the result in a histogram. E.g Number of series: 3 , Number of experiments: 4 , which outputs a vector e.g [93 78 345 234], each element representing each experiment.

7. Sep 22, 2006

### Thomas98

Maybe something like this , I ran it but it clogged up the memory:

function result = monte(p,q) % p is number of series, q is number of experiments

for i=1:q
while i<p
dice = ceil(6*rand(5,1));
A=(dice.*(dice~=mode(dice)));
k=1;
while any(dice.*(dice~=mode(dice)))~=0
dice(find(A))=ceil(6*rand(size(find(A))));
k=k+1; %Calculate number of re-throws
end
end
end