Search code examples
javamathpermutation

All possible permutations of dice rolls


I want to write a program that simulates the following: I have 6 dice, and I roll with some dice every time.

When I don't roll with a die, I just assume that I rolled 0 with that.

I want to list all the possible variations I can get this way. A few examples:

1,2,4,6,0,1

3,5,1,0,0,4

6,6,4,2,0,0 etc.

Any ideas on how to do this? (I am using Java, but of course, I'm only interested in the general concept.)


Solution

  • You can use a recursive method, keeping track of depth: EDIT, maybe this method would be better:

    class Main {
        public static void roll(String s, int depth) {
            if(depth == 0)
                System.out.println(s.substring(1));
            else
                for(int i = 0; i <= 6; i++)
                    roll(s + "," + i, depth - 1);
        }
        public static void main(String[] args) {
            roll("", 6); //2nd parameter > 0
        }
    }