# Question about Randoms and trying to use it

• September 29th, 2013, 12:14 PM
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.

:confused:

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*/
• September 29th, 2013, 12:18 PM
GregBrannon
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!
• September 29th, 2013, 12:37 PM
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
• September 29th, 2013, 01:08 PM
GregBrannon
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:
Code java:

```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;```
• September 29th, 2013, 02:09 PM
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.
• September 29th, 2013, 02:13 PM
GregBrannon
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.
• September 29th, 2013, 03:54 PM
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.
• September 29th, 2013, 04:03 PM
GregBrannon
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."
• September 29th, 2013, 04:26 PM
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")

}
}
• September 29th, 2013, 04:37 PM
GregBrannon
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.