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 10 of 10

Thread: Question about Randoms and trying to use it

  1. #1
    Junior Member adipietro's Avatar
    Join Date
    Sep 2013
    Location
    Bel Air, MD
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Question about Randoms and trying to use it

    I am extremely new to programming and I have an issue trying to use a random. I am not looking for the direct answers, but maybe some guidance. I am working on an exercise that is supposed to help me learn the language, but I am struggling with trying to create a random that i need to call upon later in the program to create an average. I am supposed to create a program that allows a user to enter two dice sides. Then the system randomly rolls each dice multiple times and at the end averages the number rolled for each die. My program has a lot of comments so i can review it and know what function each line performs. Any assistance is greatly appreciated.



    Thanks,
    Anthony

    import java.util.Random;
    import java.util.Scanner;

    public class Dice {

    public static void main(String[] args) {

    //int is the Primitive Data type and Diceone and Dicetwo are the Variables I created
    int Diceone, Dicetwo;

    /**int is the Primitive Data type and the DiceonefirstRoll is the variable for holding the number entered by the user*/
    int DicetwofirstRoll, Dicetwosecondroll, DicetwothirdRoll;

    //Calls the scanner class
    Scanner scan = new Scanner (System.in);

    //Sets Diceone to be the next integer entered via the keyboard
    System.out.print ("How many sides does die 1 have?");
    Diceone = scan.nextInt();

    //Sets Diceotwo to be the next integer entered via the keyboard
    System.out.print ("How many sides does die 2 have?");
    Dicetwo = scan.nextInt();

    //Closes the scanner for input
    scan.close ();

    //Do I need to call the random class like I do with the scanner in line 16?

    //Creates random dice roll
    System.out.print ("Die 1 first roll=");
    Random DiceonefirstRoll= new Random(Diceone) + 1;
    /**Trying make this so i can call it later. I am trying to have the # entered from Diceone and +1 due to not being able to receive a 0 on a die*/


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Question about Randoms and trying to use it

    Welcome to the forum! Be sure to read the "Announcement" topic at the top of most of the sub-forums for pointers on how to post correctly and get help.

    I think it's great that you start your code writing with lots of comments. I recommend you continue that practice, but different strokes for different folks.

    But I don't understand your question or even if there is a question. One would not typically use a Random instance to "create and average." You might use a Random instance to simulate rolling the dice, however. Please think through what you're asking or need help with and ask a specific question.

    Good luck!

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    adipietro (September 29th, 2013)

  4. #3
    Junior Member adipietro's Avatar
    Join Date
    Sep 2013
    Location
    Bel Air, MD
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Question about Randoms and trying to use it

    I am trying to make it so the program will randomly roll the dice and display its roll. I need to Perform this task three times. Then i need to average the total of the three rolls and display the total.

    This is my first time trying to use a random. I am trying to determine how to get a random # +1 from the information provided by the user for Diceone and name the number provided by the random so i can call it out again for the average later in the program. I can not use exact numbers though because everything can change each time the sides of a die are changed.

    code in English:
    (Name of First roll to call out later) = Random # for number of sides die one has

    does this all make sense?

    thanks,
    anthony

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Question about Randoms and trying to use it

    Pretty much makes sense, because it's been done many times. You might figure it out by reading the Random API page, but I'm not sure. Here are some hints:
    Random random = new Random();
     
    // to get a random dice roll from 1 through 6 and store in the variable diceRoll:
    int diceRoll = random.nextInt( 6 ) + 1;

  6. #5
    Junior Member adipietro's Avatar
    Join Date
    Sep 2013
    Location
    Bel Air, MD
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Question about Randoms and trying to use it

    Greg,
    I didn't want to waste anyone's time prior to writing my post so i reviewed Oracles API page and googled some, but had little luck getting my IDE to approve of the code I was typing. I tried something very similar to what you sent me, but I substituted my dice roll for the 6 and it errors. I have tried many ways but this is the last two I have tried:

    int DiceonefirstRoll= Random(Diceone) + 1;

    and

    I added DiceonefirstRoll to my delared integers at the top of the program above the class for this one:

    DiceonefirstRoll= Random(Diceone) + 1;

    Should I look at maybe using the math class instead of random because i am trying to use variables instead of integers?

    Greg thank you for the assistance and your quick responses.

  7. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Question about Randoms and trying to use it

    I can think of a couple reasons why what you posted would produce errors, but to help you I need to know what all of your variables are and the exact error, copied and pasted from just as it appears at your end. No, you shouldn't change to Math.random() just because you're having difficulty figuring this out.

  8. #7
    Junior Member adipietro's Avatar
    Join Date
    Sep 2013
    Location
    Bel Air, MD
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Question about Randoms and trying to use it

    i keep trying to attach the screen shot of my code and the error, but the web forum keeps freezing when i select attach image. I have been continuing for hours to find a way to make it work. i'll post my most resent code and issues by 6:00 pm. because i am trying to figure out if i need to set constants and how to set them.

  9. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Question about Randoms and trying to use it

    Screen shots suck. Copy and paste the code and the error.

    When you get the constant hourglass or loading indicator, it's not a freeze but a forum bug that is being worked. Right click on the button you're trying to use and select, "Open in a new tab."

  10. #9
    Junior Member adipietro's Avatar
    Join Date
    Sep 2013
    Location
    Bel Air, MD
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Question about Randoms and trying to use it

    this is my most recent code:

    import java.util.Random;
    import java.util.Scanner;

    public class Dice {

    /**Created two public static in constants for Diceone and Dicetwo due to random displaying an error when using something that isn't static*/
    public static int Diceone;
    public static int Dicetwo;

    public static void main(String[] args) {

    /**int is the Primitive Data type and the DiceonefirstRoll is the variable for holding the number entered by the user*/
    final int DiceonefirstRoll, DiceonesecondRoll, DiceonethirdRoll;
    final int DicetwofirstRoll, Dicetwosecondroll, DicetwothirdRoll;

    //Calls the scanner class
    Scanner scan = new Scanner (System.in);

    //Sets Diceone to be the next integer entered via the keyboard
    System.out.print ("How many sides does die 1 have?");
    Diceone = scan.nextInt();

    //Sets Dicetwo to be the next integer entered via the keyboard
    System.out.print ("How many sides does die 2 have?");
    Dicetwo = scan.nextInt();

    //Closes the scanner for input
    scan.close ();

    //Do I need to call the random class?

    //Creates random dice roll
    System.out.print ("Die 1 first roll=");
    DiceonefirstRoll = Random.nextInt(Diceone) + 1; (provides the error "Cannot make a static reference to the non-static method nextInt(int) from the type Random")

    }
    }

  11. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Question about Randoms and trying to use it

    Learn to post your code in code tags (see post #2). No more help (after this) until you do.

    Variable and method names should begin with lowercase letters and then camel-cased. Diceone should be diceOne and Dicetwo should be diceTwo.

    Why didn't you make a Random object, random, as I suggested? That would eliminate the error.