How do i hold a number from a randi
4 views (last 30 days)
Show older comments
Hey, I have to create a dice rolling code for a Yahtzee game and I don't know how to hold the dice so that the rest can be re-rolled. Additionally the version my group is making is not text based it has a UI. Thank You in advance.
1 Comment
James Tursa
on 11 Apr 2018
How are you holding the current roll numbers? In a 5-element variable with element values 1 to 6?
How is the user selecting the dice to re-roll? In a vector of 0 to 5 elements with numbers 1 to 5? Or ...?
Answers (1)
John D'Errico
on 11 Apr 2018
Well, if you've gotten to the point of knowing how to use randi, I might as well give you a hint.
Dice = randi(6,[1 5])
Dice =
3 5 4 4 6
I could go for a straight now, or for 3 or 4 of a kind in 4s.
replacements = [1 2 5];
Dice(replacements) = randi(6,[1,numel(replacements)])
Dice =
2 5 4 4 5
Try for another 4 or a 5.
replacements = [1];
Dice(replacements) = randi(6,[1,numel(replacements)])
Dice =
3 5 4 4 5
Looks like 2 pairs to me. Maybe I should have tried for the straight instead?
1 Comment
John D'Errico
on 13 Apr 2018
Edited: John D'Errico
on 13 Apr 2018
If you have a list of dice that you want to hold, use setdiff.
replace = setdiff(1:5,hold);
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!