필터 지우기
필터 지우기

How can I rite a program to roll x number of dice and take the lowest value out while adding the rest?

조회 수: 3 (최근 30일)
Ok so my friends and I from college are setting up a game of D&D online (because its summer and we are scattered across the country). We are rolling for character stats soon and, in case you are not familiar how its done, you roll 4d6 (four six-sided dice) taking out the lowest value then adding the rest up to give you a combined value range from 3 to 18.
Now I figured out this
A=randi([1 6],1,4)
This will give me a 1x4 random matrix of integers from 1 to 6. I could add a sum command in front to add them all up like this
B=sum(randi([1 6],1,4))
But that will just add up four integers, when I only want the three highest values added up and the lowest thrown out.
Is there a simple way to make a random 1x4 matrix of integers, take out the lowest value, then add the rest?
  댓글 수: 1
Craig
Craig 2013년 6월 23일
Thinking about it more, maybe i could use a min command to find the minimum in the matrix, replace it with a 0, then add up the remaining integers. That would work…I am just not sure how to do it.

댓글을 달려면 로그인하십시오.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 6월 23일
편집: Azzi Abdelmalek 2013년 6월 23일
A=randi([1 6],1,4)
B=sum(setdiff(A,min(A)))
%or
B=sum(A)-min(A)
  댓글 수: 1
Craig
Craig 2013년 6월 23일
Yeah I just figured that one out. I got to focused on trying to learn how to replace the lowest value with a 0 that I completely missed the simple solution -_-"
Anyways thank you so much.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Calendar에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by