Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Dice Rolling Simulation

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    14
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Dice Rolling Simulation

    I have coded a dice rolling simulation to roll a single die 36,000 times, store the data in the array and display how many times the face value landed. My question is what do I need to do to change this to simulate rolling 2 die to display the output of 2-12 face value, so adding the face value of the 2 randomly rolled die and incrementing face value. Here is the code I have for the single die:
    // Dice Rolling
     
    import java.util.Random;
     
    public class DiceRolling
    {
    	public static void main( String[] args)
    	{
    		Random randomNumbers = new Random(); // Generates random numbers
    		int[] array = new int[ 7 ]; // Declares the array
     
    		//Roll the die 36,000 times
    		for ( int roll = 1; roll <=36000; roll++ )
    			++array[ 1 + randomNumbers.nextInt ( 6 ) ];
     
    		System.out.printf( "%s%10s\n", "Face", "Frequency" );
     
    		// outputs array values
    		for ( int face = 1; face < array.length; face++ )
    			System.out.printf( "%4d%10d\n", face, array[ face ] );
     
    	} // end main
     
    } // end class DiceRolling


  2. #2
    Junior Member
    Join Date
    Jan 2011
    Posts
    14
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Dice Rolling Simulation

    I think I may have a little more information but now I'm getting a dice1 cannot be initialized and I do not believe I am totalling up the two correctly to store them back into the array.
    // Dice Rolling

    import java.util.Random;

    public class DiceRolling
    {
    public static void main( String[] args)
    {
    Random randomNumbers = new Random(); // Generates random numbers
    int[] array = new int[ 13 ]; // Declares the array
    int dice1;
    int dice2;
    int total;

    //Roll the die 36,000 times
    for ( int roll = 1; roll <=36000; roll++ )
    dice1 = 1 + randomNumbers.nextInt ( 6 );
    dice2 = 1 + randomNumbers.nextInt ( 6 );
    total = dice1+dice2;
    ++array[total];

    System.out.printf( "%s%10s\n", "Face", "Frequency" );

    // outputs array values
    for ( int face = 1; face < array.length; face++ )
    System.out.printf( "%4d%10d\n", face, array[ face ] );

    } // end main

    } // end class DiceRolling

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    14
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Dice Rolling Simulation

    Solved:
    //Roll the die 36,000 times
    		for ( int roll = 1; roll <=36000; roll++ )
    		{	
    			int die1 = 1 + randomNumbers.nextInt ( 6 );
    			int die2 = 1 + randomNumbers.nextInt ( 6 );
    			int total = die1 + die2;
    			++array[total];
     
    		}

Similar Threads

  1. Monty Hall Problem Simulation
    By caseyd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 5th, 2011, 01:31 AM
  2. Trying to achieve Movie Style Rolling Credits
    By javanoob123 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 10th, 2011, 08:02 PM
  3. Die Rolling - Continuing the statement?
    By Override in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 29th, 2010, 01:21 AM
  4. Dice Program Help
    By Bradshjo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 2nd, 2010, 07:50 AM
  5. Keypress simulation problems
    By ummix in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 6th, 2010, 07:31 AM