Simple help with matlab-dice game needed

  • MATLAB
  • Thread starter Thomas98
  • Start date
  • Tags
    Game
In summary, Thomas0.'s code includes a dice function to throw a set of six random dice, and a while-loop to check if the dice are all the same value and then save the dice with the most number of throws. He also wants to add a function to repeat the process until all dice are of the same value.
  • #1
Thomas98
5
0
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
 
Physics news on Phys.org
  • #2
0. "Die" is the singular of "dice". One die, two dice.

1.
Code:
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
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
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
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
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
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
 

Related to Simple help with matlab-dice game needed

1. What is the objective of the matlab-dice game?

The objective of the matlab-dice game is to roll a pair of dice and try to get a higher total number than your opponent. The player with the highest total number wins the game.

2. How do I run the matlab-dice game?

To run the matlab-dice game, you will need to have the matlab software installed on your computer. Once you have matlab open, you can simply type in the code for the game and press run. The game will then start and you can follow the prompts to play.

3. Can I customize the rules of the matlab-dice game?

Yes, you can customize the rules of the matlab-dice game by modifying the code. You can change the number of dice, the target number to win, and any other rules you would like to add or change.

4. How does the computer determine the outcome of the matlab-dice game?

The computer uses a random number generator to simulate the rolling of the dice. The total number of the two dice is then compared to the opponent's total number, and the player with the higher number wins.

5. Is there a way to improve my chances of winning in the matlab-dice game?

Since the outcomes of the game are determined by random chance, there is no guaranteed way to improve your chances of winning. However, you can try different strategies such as betting on specific numbers or using probability calculations to increase your chances of winning.

Similar threads

  • Set Theory, Logic, Probability, Statistics
Replies
6
Views
1K
  • Precalculus Mathematics Homework Help
2
Replies
53
Views
6K
  • Set Theory, Logic, Probability, Statistics
2
Replies
41
Views
4K
Replies
8
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
4K
  • Set Theory, Logic, Probability, Statistics
2
Replies
42
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • Precalculus Mathematics Homework Help
Replies
11
Views
2K
Back
Top