Computer program: dice simulator, suggestions for improvement? (CODE in JAVA) Watch

Vardhan95
Badges: 14
Rep:
?
#1
Report Thread starter 3 weeks ago
#1
Hi all,

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.
Spoiler:
Show
/**
* 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
*/

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

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
other methods.

private class Sample
{

// 6 integer tuple for 6 dice rolled, the length of the tuple will be equal to the number of dice rolled.
int a;
int b;
int c;
int d;
int e;
int f;

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 )
{
return 1;
}
else
{
return 0;
}
}

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;

// Constructor
public Lotto()
{
space = new ArrayList<Sample>();
generator = new Random();
enumerateSpace();
}

//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);
space.add(addend);
} } } } } } }

// 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)
{
counter++;
}
}
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 + " ");
returned.printSample();
sum += returned.sum();
Collections.shuffle(space);
}

avg = sum / n;
System.out.println("The average value of the sum of these rolls is: " + avg);

}

}
Last edited by Vardhan95; 3 weeks ago
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

University open days

  • Bournemouth University
    Undergraduate Open Day Undergraduate
    Wed, 19 Feb '20
  • Buckinghamshire New University
    Postgraduate and professional courses Postgraduate
    Wed, 19 Feb '20
  • University of Warwick
    Warwick Business School Postgraduate
    Thu, 20 Feb '20

Has your university offer been reduced?

Yes (45)
31.69%
No (74)
52.11%
Don't know (23)
16.2%

Watched Threads

View All