I'm dying here! So far you guys have been way more help than my instructor who speaks Russian or something. Any advice will be more than helpful.
This is my issue...
Use the random class once for each die, the sum of the two values should then be calculated. Each die can show an int value from 1-6 so the sum with vary from 2-12 with 7 being the most frequent. The dice should roll 36,000 times. I have to use a 1 dimensional array to tally the number of times each sum appears. Results need to be displayed in a tabular format.
I don't know what I'm doing wrong but I'm positive the results are so wrong!

``````import java.util.Random;

public class dice{
public static int rollnumber;
public static int ROLL_LIMIT = 36000;
public static int[] die1= new int[ROLL_LIMIT];
public static int[] die2= new int[ROLL_LIMIT];

//This function keeps track of how many rolls
//have occured and returns false if it reaches
//ROLL_LIMIT
public static boolean rolltrack(){
++rollnumber;
if(rollnumber == ROLL_LIMIT) {
return false;
}
else {
return true;
}
}
//Generates a random number for each array
//of die numbers. The numbers that are generated
//aren't very random, probably due to the high speed
//at which the function is continuously called.

public static void rollDice(){
Random rand = new Random();
die1[rollnumber] = rand.nextInt(6)+1;
die2[rollnumber] = rand.nextInt(6)+1;
}
//This function calculates the statistic of how much
//each number was rolled for each die, then outputs
//the data as text

public static void tell_roll_statistics()
{
int[] number_rolls1=new int[6];
int[] number_rolls2=new int[6];
double[] number_percent1=new double[6];
double[] number_percent2=new double[6];
for(int j=0;j<6;j++){
for(int k=0;k<ROLL_LIMIT;k++){
if(die1[k] == (j+1)) {
++number_rolls1[j]; //Holds the amount of rolls for each number
}//Holds the amount of rolls for each number
if(die2[k] == (j+1)) {
//in the array number_rolls_percent1 and 2
++number_rolls2[j];
}
}
number_percent1[j] = ((double)number_rolls1[j]/ROLL_LIMIT)*10 ;
number_percent2[j] = ((double)number_rolls2[j]/ROLL_LIMIT)*10 ;
System.out.println("[Die1] % of number "+(j+1)+": "+number_percent1[j]);
System.out.println("[Die2] % of number "+(j+1)+": "+number_percent2[j]);
}
}

public static void main(String args[]){
rollnumber = 0;
do{
rollDice();
}while(rolltrack());
tell_roll_statistics();
}
}
``````

Edited by Dani: Formatting fixed

4
Contributors
4
Replies
8
Views
8 Years
Discussion Span
Last Post by CTUBren

I think you'r missing the point! They didn't want the dice history, only the statistics of the sums!

``````int  sum[ 11 ];
// Zero sum array[]
loop 36000 times
dice1 = random  0...5
dice2 = random 0...5
sum[ dice1 + dice2 ]++;    // 0...10
end loop``````

Since you're building a class for a die, you could insert a debug statistics function that tracks that die's stats of each roll. int stat[6];
(Maybe it'll be extra credit for additional work!)
Flip a coin 100 times it won't necessarily be 50 heads and 50 tails and 0 edges.
Now flip it 50,000 times. It most likely won't be 50.0000% heads.

Since its a class pass an integer as part of the constructor with a default of 6. In that way your class can be used for {4...N} sided die! Thus flexixble!

Code tags:

[code=JAVA]
paste code here
[/code]

36,000 separate Random objects?

``````public static void rollDice(){
Random rand = new Random();
die1[rollnumber] = rand.nextInt(6)+1;
die2[rollnumber] = rand.nextInt(6)+1;
}
``````

Edited by happygeek: fixed formatting

Off topic: Just do not tell me you are from Westminster University or I start laughing. This is traditional assignment there.

Wildgoose, I can't believe I'm trying to make these codes so difficult. I'm going to re-work my code and see how it works. Looks like you're right, but that's why I came here instead of asking the instructor at school.
Thank you,

Bren

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.