English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am supposed to create this program that rolls dice 36000 times. After the "dice roll", I am supposed to make a one dimensional array that displays the number of times each possible sum of the dice appears....i.e. 3 was rolled 1,000 times and 4 was rolled 4,000 times etc. Right now, when I run the program, 2-6 have results in the 12,000 range and 7-12 have 1 each. Please Help.
import java.util.Random;

public class RollDice
{
// main method begins program execution
public static void main( String args[] )
{
Random randomNumbers = new Random(); // random number generator
// one-dimensional array of possible sums
int totarray[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
int frequency[] = new int[ 13 ]; //array of frequency counters
// roll dice 36000 times;
for ( int rolldie1 = 1; rolldie1 <= 36000; rolldie1++ )
++frequency[ 1 + randomNumbers.nextInt( 6 ) ];
for (int rolldie2 = 1; rolldie2<= 36000; rolldie2++ )
++frequency[ 1 + randomNumbers.nextInt( 6 ) ];
for ( int answer = 0; answer < totarray.length; answer++ )
++frequency[ totarray[ answer ] ];
for ( int tots = 2; tots < frequency.length; tots++ )
System.out.printf( "%d%10d\n", tots, frequency [ tots ] );
}
}

2007-03-27 07:24:18 · 4 answers · asked by Anonymous in Computers & Internet Programming & Design

4 answers

I don't understand why you would have an array of size 13? There are only 11 possible answers.

Try this:

// main method begins program execution
public static void main( String args[] )
{
Random rand = new Random();

// one-dimensional array of possible sums
int rollsums[] = new int[11];
int die1, die2;

// roll dice 36000 times;
for( int rolls = 0; rolls < 36000; rolls++ ) {
die1 = rand.nextInt( 6 ) + 1;
die2 = rand.nextInt( 6 ) + 1;
++rollsums[ die1 + die2 - 2 ];
}

for( int i=0; i< rollsums.length; i++ ) {
System.out.printf( "%d\t%d\n", i+2, rollsums[i] );
}
}

2007-03-27 07:54:20 · answer #1 · answered by javier 2 · 0 0

I think you need two loops since it says to have each possible sum. So this means that if 1 was rolled 10 times and 3 was rolled 10 times then 4 should 20. Right?
so in this case you need two loops. and another array.

so do
int sums[] = new int[];

then for the third loop do
for(int i =0; i < frequency.length; i++)
{
for(int j =0; j < frequency.length; j++){
if(i > j){ //this way we don't add the numbers we already added -- go through code on paper.
sums[i+j] = frequency[i]+frequency[j];
}
}
}


Also, I don't see a reason to have a second loop since you only need to roll the dice 36000. In your program you are doing it twice.

Also frequency needs to be only 6 long not 13 .. since with my code you will have a third array.

At the end just print out the sums array.

Another thing - get rid of totarray or substitute it with sums array I propose and declared the way I did.

Also change the line ++frequency[ 1 + randomNumbers.nextInt( 6 ) ]; to
++frequency[ randomNumbers.nextInt( 6 ) ];

OK, I think that should work. So good luck. I hope this makes sense.

2007-03-27 15:13:24 · answer #2 · answered by demaman 3 · 0 0

i really don't anderstand the idea ur prog depends on...but i did some fixing, so here it goes:
import java.util.Random;

public class RollDice
{
// main method begins program execution
public static void main( String args[] )
{
Random randomNumbers = new Random(); // random number generator
// one-dimensional array of possible sums (yes only one!)
int frequency[] = new int[ 13 ];

for ( int rolldie1 = 1; rolldie1 <= 36000; rolldie1++ )
++frequency[ 1 + randomNumbers.nextInt( 6 )+1 + randomNumbers.nextInt( 6 )
//this way, each iteration u run randomNumbers.nextInt() twice as if rolling the die twice, then depending on their sum u add to the designated counter in the array

for ( int tots = 2; tots < frequency.length; tots++ )
System.out.printf( "%d%10d\n", tots, frequency [ tots ] );
}
}
//and voila!

2007-03-27 15:00:10 · answer #3 · answered by Khaled Z 3 · 0 0

I would recommend just having one array of 13 numbers initialized to 0.

1. Roll two dice like you do with the random generator.
2. If the sum is 5, increment the array's 5th item (or 6th item, however you want to do the counting).
That is frequency[5]++.

At the end, loop through the frequency array and just get the integer that corresponds to each index, which corresponds to all the sums from 2 to 12.

Of course frequency[0] and frequency[1] should be zero or you have some bug.

2007-03-27 14:43:26 · answer #4 · answered by michmounty 2 · 0 0

fedest.com, questions and answers