• Status: Solved
• Priority: Medium
• Security: Public
• Views: 210

# Given "n"(n>0) number of dices with "m"(m>0) sides. Print all possible combinations

Hi,
Write a Java program to print all possible combinations in below case
"n"(n>0) number of dices with "m"(m>0) sides. Print all possible combinations
for example.
2 dices with 3 sides should print out this
(1,1)
(1,2)
(1,3)
(2,1)
(2,2)
(2,3)
(3,1)
(3,2)
(3,3)
I gave it a sincere try before posting this. My code doesn't work if number of dices is more than 2 which is not right. Thanks in advance for looking.
0
koppcha
• 4
• 2
• 2
• +3
3 Solutions

IT Business Systems Analyst / Software DeveloperCommented:

Also, I assume that this is for a homework/assignment exercise, but can you please confirm if that is the case? This allows everyone to provide the correct level of assistance.
0

Author Commented:
This is definitely not homework/assignment.  It's been long time since I have been to school(which could be the reason I spent half day figuring this out )

``````import java.util.Arrays;

public class DiceHistogram {
public void getHistogram(int dices, int sides) {
int diceArray[] = new int[dices];

//Initialize Array
for (int i = 0; i< diceArray.length; i++) {
diceArray[i] = 1;
}

//System.out.println(Arrays.toString(diceArray));
for(int k=dices-2; k>=0; k--) {
for(int j=1; j<= sides; j++) {
diceArray[k] = j;
for(int i=1; i<=sides; i++ ) {
diceArray[dices-1] = i;
System.out.println(Arrays.toString(diceArray));

}

}
}

}

//for ( int j)

public static void main(String[] args) {
DiceHistogram dh = new DiceHistogram();
dh.getHistogram(2, 3);
}

}
``````
0

Commented:
Often, when the term "combinations" is used in an exercise, the order does not matter, so (2,1) is considered the same as (1,2).
0

Author Commented:
In this case they should be considered different. "Combination" is my own way of explaining it not part of original problem. Example should be taken for reference. They are considered different.
0

Commented:
Can we assume that pow(m,n) fits in an int?
0

Commented:
This is actually a fairly tricky problem.  You can't just use one or two or even 5 loops to do it, but you really need 1 loop for each dice - which is a parameter to the problem.

That sort of problem is usually easiest to solve with recursion - where a function calls to itself.

Something like this.

Recursive functions tend to be not a lot of code, but can be hard to understand if you've not seen them before.  I added some comments to help explain what's going on.

Doug

``````	public static List<String> dice(int nDice, int nSides) {
List<String> combinations = new ArrayList<>();

if (nDice == 1) {
// For one dice we get a list with just 1 entry
// 1
// 2
// 3
for (int side = 1; side <= nSides; side++) {
}
} else {
// When we have more than one dice we add the values for this dice
// to all possible combinations from "nDice-1"
// So add "1," to all subsequent combinations (e.g. "1,1", "1,2", "2,1", "2,2" etc.)
List<String> following = dice(nDice-1, nSides) ;
for (int side = 1; side <= nSides; side++) {
for (String follow : following) {
combinations.add(side + ", " + follow);
}
}
}

return combinations ;
}

public static void listCombinations() {
List<String> combinations = dice(4, 3) ;

for (String combo : combinations) {
System.out.println("(" + combo + ")") ;
}
}
``````
0

Author Commented:
Hi Dpearson,
I tried your program with different inputs and it works as expected. I am still trying to understand. will get back to you. Thanks for your help.

Hi Ozo,
Yes I think it's fair assumption.
0

Commented:
if pow(sides,dices) fits in an int, another method might be
static void getHistogram(int dices, int sides) {
for( int i=(int)Math.pow(sides,dices);--i>=0;){
System.out.print("(");
int n=i;
for( int d=0;d<dices;++d ){
if( d>0 ){ System.out.print(","); }
System.out.print(sides-(n%sides));
n/=sides;
}
System.out.println(")");
}
}
0

Commented:
http://www.mathsisfun.com/combinatorics/combinations-permutations.html
Here is my version.  What you call a side, I call a face.
``````import java.util.*;
public class Dice{
public static void main(String[] args) {
int numberOfDice = 3;
int numberOfFaces = 3;
int numberOfOutcomes = (int)Math.pow(numberOfFaces,numberOfDice);
int[] roll = new int[numberOfOutcomes]; //all initially 0 (roll to the next face)
StringBuilder[] outcomes = new StringBuilder[numberOfOutcomes];
for(int j=0; j<numberOfOutcomes; j++){
outcomes[j] = new StringBuilder("(");
}
for(int k = 1; k <= numberOfDice; k++){
int numberOnFace = 1;
for(int y = 0; y < numberOfOutcomes; y++){
if(roll[y] == 0){
outcomes[y].append(numberOnFace);
if(k != numberOfDice)outcomes[y].append(",");
numberOnFace++;
roll[y] = 1; // don't roll, just stay on the same face
if(numberOnFace == numberOfFaces + 1){
numberOnFace = 1;
roll[y] = 0; // roll to the next face
}
}else{
outcomes[y].append(numberOnFace);
if(k != numberOfDice)outcomes[y].append(",");
}
}
}
for(int z = 0; z < numberOfOutcomes; z++){
System.out.println(outcomes[z].toString() + ")");
}
}
}
``````
0

Commented:
I am still trying to understand. will get back to you. Thanks for your help.

The key for a recursive solution is to understand that the larger problem can be seen as a repetition of a smaller problem.

E.g. If I gave you a list of all combinations of 2 dice with 3 sides:

(1,1)
(1,2)
(1,3)
(2,1)
(2,2)
(2,3)
(3,1)
(3,2)
(3,3)

then you can use this list to create the list of all combinations of 3 dice by adding "1, ...", "2, ..." and "3, ..." to the front of each of the earlier combinations:

1 +  (1,1) => (1,1,1)
1+  (1,2) => (1,1,2)
1 + (1,3) => (1,1,3)
1 + (2,1) => (1,2,1)
...
3 + (1,1) => (3,1,1)
3 + (1,2) => (3,1,2)
...
etc..

Once you recognize the problem has that property, then you can write a function that calls to itself to get it to compute this.

Anyway - hope you can figure it out :)

Doug
0

Author Commented:
Hi Doug,
I got it now. Thanks for the help.

Ozo/rrz,
Thanks guys. Your code works too.

Doug's solution is easier for me to understand.
I am going to close this question now.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.