Computer program: dice simulator, suggestions for improvement? (CODE in JAVA) Watch
so I have made this 6 dice roll simulator in JAVA, but I feel the code is inefficient as to create the sample space it needs 6 for loops.
In general, according to my program, for N dice we need N loops, however with N loops the runtime becomes N^2. I am wondering if there is any way to improve the runtime, WITHOUT changing whats stored in the sample space.
enumerateSpace() is the method, the code is below in the spooiler and it is commented.
* Lotto class. Consists of Lotto, with inner class Sample, Lotto contains a field that is an arraylist that stores Sample objects.
* This lotto class provides all outcomes of rolling 6, 6 sided dice. It can be extended to more dice, and with more sides.
* @author Vardhan
* @version 1.3
public class Lotto
// The inner class sample, which is basically supposed to be the tuple, of length 6 integers. It provides the constructor, sum and some
private class Sample
// 6 integer tuple for 6 dice rolled, the length of the tuple will be equal to the number of dice rolled.
private Sample(int a, int b, int c, int d, int e, int f)
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
this.f = f;
private void printSample()
System.out.println( this.a + "," + this.b + "," + this.c + "," + this.d + "," + this.e + "," + this.f + "," );
private int matchEqualsOrNot( Sample s )
if( s.a == this.a && s.b == this.b && s.c == this.c && s.d == this.d && s.e == this.e && s.d == this.d && s.e == this.e && s.f == this.f )
private int sum()
return (this.a + this.b + this.c + this.d + this.e + this. f );
// The arraylist which stores all the outcomes of rolling the dice.
private ArrayList<Sample> space;
space = new ArrayList<Sample>();
generator = new Random();
//Enumerate space method: This creates the sample space, using nested for loops that range from 1 to 6, 6 of these, creating new
//sample objects. Hence, all 46656 outcomes of rolling 6 dice are created and saved in the arraylist.
private void enumerateSpace()
for( int a = 1; a <= 6; a++)
for( int b= 1; b <= 6; b++ )
for( int c = 1; c <= 6; c++)
for( int d = 1; d <= 6; d++)
for( int e = 1; e <= 6; e++)
for( int f = 1; f <= 6; f++)
Sample addend = new Sample(a,b,c,d,e,f);
} } } } } } }
// This method getProbability returns the probability of the outcome you enter.
public void getProbability(int a, int b, int c, int d, int e, int f)
Sample match = new Sample(a,b,c,d,e,f);
int size = space.size();
int counter = 0;
int sum = match.sum();
for( int z = 0; z <= size - 1 ; z++ )
if( space.get(z).sum() == sum)
double p = (double) counter / (double) size ;
System.out.println("The probability of such a sample is: " + p + " Or as a fraction " + counter + " / " + size);
//Roll N times method: This simply prints out the outcomes of rolling the 6 dice N times, which is inputted by the user. It reshuffles
// the sample space after each roll to ensure maximum randomness.
public void rollNtimes(int n)
int sum = 0;
double avg = 0;
for(int i = 1; i <= n; i ++)
Sample returned = space.get( generator.nextInt( space.size() - 1 ) );
System.out.println(" Roll number: " + i + " ");
sum += returned.sum();
avg = sum / n;
System.out.println("The average value of the sum of these rolls is: " + avg);