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;
}
}
4 Replies - 723 Views - Last Post: 16 November 2011 - 08:13 AM
#1
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
Replies To: multiple dice roll program can't figure out right array approach
#2
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
diceArr[i++] = myNumber.nextInt(6);
and nextInt(6) means that your values will be between 0 and 5 inclusive
#3
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
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
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.
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
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|