4 Replies - 723 Views - Last Post: 16 November 2011 - 08:13 AM Rate Topic: -----

#1 julianB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

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

Reputation: 8066
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 cornetto456  Icon User is offline

  • D.I.C Regular

Reputation: 21
  • View blog
  • 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

View PostjulianB, 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);

?
Was This Post Helpful? 0
  • +
  • -

#4 julianB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1