# multiple dice roll program can't figure out right array approach

Page 1 of 1

## 4 Replies - 723 Views - Last Post: 16 November 2011 - 08:13 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=255864&amp;s=d85a2c28db495dadcacc671f88d17402&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 julianB

Reputation: 0
• Posts: 3
• Joined: 15-November 11

# multiple dice roll program can't figure out right array approach

Posted 15 November 2011 - 10:24 PM

I wrote a method which assigns random numbers to an array and them ads them together as one roll. Then I increment another frequency array in same function depending on which number the roll is. I need to then print this frequency counter out along with the average over all the rolls. I just want a push in the right direction, or if there is anything that is obviously wrong that would help too
```import java.util.Scanner;
import java.util.Random;
public class DiceStats {

static Random myNumber = new Random();
public static int numberDice = 0, numberRolls = 0, numberCounters = 0;
public static int[] diceArr;
public static int[] rollArr;
public static int[] counterArr;

public static void main( String [] args)
{

Scanner input = new Scanner(System.in);
System.out.println(" How many di would you like to roll?");
numberDice = input.nextInt();
System.out.println("How many times would you like to roll your dice");
numberRolls = input.nextInt();
numberCounters = numberDice*6;
int[] diceArr = new int[numberDice];
int[] rollArr = new int[numberRolls];
int[] counterArr = new int[numberCounters];

for(int x = numberRolls; x>0; x--)
{
rollArr[x] = createRandom();
++counterArr[createRandom()];
}
System.out.println("Sum     # of times      Percentage");

for ( int totalsum = numberDice; totalsum <= rollArr.length; totalsum++ )
System.out.printf( "%4d%10d\n", totalsum, rollArr[ totalsum ] );

}

public static int createRandom()
{
int sum = 0;
for (int i = 0; i <= numberDice; i++ )
diceArr[i++] = myNumber.nextInt(6);

for(int y = 0; y <= diceArr.length; y++)
sum =+ diceArr[y];
return sum;
}

}
```

Is This A Good Question/Topic? 0

## Replies To: multiple dice roll program can't figure out right array approach

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8066
• Posts: 31,310
• Joined: 06-March 08

## Re: multiple dice roll program can't figure out right array approach

Posted 15 November 2011 - 11:05 PM

What does the i++ does in that statement

diceArr[i++] = myNumber.nextInt(6);

and nextInt(6) means that your values will be between 0 and 5 inclusive

### #3 cornetto456

• D.I.C Regular

Reputation: 21
• Posts: 438
• Joined: 03-January 11

## Re: multiple dice roll program can't figure out right array approach

Posted 16 November 2011 - 02:47 AM

julianB, on 15 November 2011 - 10:24 PM, said:

I wrote a method which assigns random numbers to an array and them ads them together as one roll. Then I increment another frequency array in same function depending on which number the roll is. I need to then print this frequency counter out along with the average over all the rolls. I just want a push in the right direction, or if there is anything that is obviously wrong that would help too
```import java.util.Scanner;
import java.util.Random;
public class DiceStats {

static Random myNumber = new Random();
public static int numberDice = 0, numberRolls = 0, numberCounters = 0;
public static int[] diceArr;
public static int[] rollArr;
public static int[] counterArr;

public static void main( String [] args)
{

Scanner input = new Scanner(System.in);
System.out.println(" How many di would you like to roll?");
numberDice = input.nextInt();
System.out.println("How many times would you like to roll your dice");
numberRolls = input.nextInt();
numberCounters = numberDice*6;
int[] diceArr = new int[numberDice];
int[] rollArr = new int[numberRolls];
int[] counterArr = new int[numberCounters];

for(int x = numberRolls; x>0; x--)
{
rollArr[x] = createRandom();
++counterArr[createRandom()];
}
System.out.println("Sum     # of times      Percentage");

for ( int totalsum = numberDice; totalsum <= rollArr.length; totalsum++ )
System.out.printf( "%4d%10d\n", totalsum, rollArr[ totalsum ] );

}

public static int createRandom()
{
int sum = 0;
for (int i = 0; i <= numberDice; i++ )
diceArr[i++] = myNumber.nextInt(6);

for(int y = 0; y <= diceArr.length; y++)
sum =+ diceArr[y];
return sum;
}

}
```

Does this:diceArr[i++] = myNumber.nextInt(6);
Not ment to be this:diceArr[i] = myNumber.nextInt(6);

?

### #4 julianB

Reputation: 0
• Posts: 3
• Joined: 15-November 11

## Re: multiple dice roll program can't figure out right array approach

Posted 16 November 2011 - 08:04 AM

I was trying to increment the number form 0-5 to 1-6

### #5 Fuzzyness

• Comp Sci Student

Reputation: 669
• Posts: 2,438
• Joined: 06-March 09

## Re: multiple dice roll program can't figure out right array approach

Posted 16 November 2011 - 08:13 AM

Then shouldnt that be: diceArr[i] = myNumber.nextInt(6) +1;?

Will give you a random 0-5, then it will add 1 to it to make it 1-6. You don't want i++, that is increasing the index it will go at. You want it like above.

This post has been edited by Fuzzyness: 16 November 2011 - 08:14 AM