Simple help with matlab-dice game needed

  • Context: MATLAB 
  • Thread starter Thread starter Thomas98
  • Start date Start date
  • Tags Tags
    Game
Click For Summary

Discussion Overview

The discussion revolves around extending a MATLAB script for a simple dice game. Participants explore how to implement additional functionalities, including determining the most represented die value, managing re-throws of non-matching dice, and simulating multiple series of identical dice throws.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related
  • Mathematical reasoning

Main Points Raised

  • One participant describes the initial functionality of their MATLAB script, which involves rolling five dice and counting the occurrences of each value.
  • Another participant suggests using the mode function to identify the most frequently rolled value and proposes filtering out non-matching values.
  • A participant encounters an issue with the mode function being absent in their MATLAB version and questions whether additional extensions are needed.
  • Further discussion includes how to structure a while loop to continue rolling until all dice show the same value, with a focus on the condition for the loop.
  • One participant shares their updated code and seeks advice on extending the script to include a Monte Carlo simulation for multiple series of identical dice.
  • Another participant provides a rough outline for a Monte Carlo function but notes that it caused memory issues during execution.

Areas of Agreement / Disagreement

Participants generally agree on the need to implement the suggested functionalities, but there are varying opinions on the specifics of the code structure and the presence of the mode function in different MATLAB versions. The discussion remains unresolved regarding the optimal implementation of the Monte Carlo simulation.

Contextual Notes

Some participants mention limitations related to the availability of certain functions in their MATLAB versions, which may affect their ability to implement suggested solutions. There are also concerns about memory management in the proposed Monte Carlo simulation code.

Who May Find This Useful

This discussion may be useful for MATLAB users interested in game development, simulation techniques, and those looking to enhance their programming skills in the context of probability and statistics.

Thomas98
Messages
5
Reaction score
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
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.
 
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?
 
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?
 
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.
 
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.
 
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
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 53 ·
2
Replies
53
Views
10K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 41 ·
2
Replies
41
Views
6K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K