Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMike Turner
13,793 PointsCreate a new int variable named randomNumber. Use the randomGenerator variable and its nextInt()
I need help.
Im stuck
Random randomGenerator; Random rand = new Random(); rand.nextInt(10);
int randomNumber = randomGenerator.nextInt();
1 Answer
Steve Hunter
57,712 PointsHi Mike,
The first question, Initialize a new Random variable called randomGenerator. Hint: Don't forget to use the new keyword. uses the code that's already there. It has declared the randomGenerator
variable name to be of type Random
you now need to create the object and assign it:
Random randomGenerator = new Random();
Next, Create a new int variable named randomNumber. Use the randomGenerator variable and its nextInt() method to get a random number. Do not set a range.. For this, you need to start with an int
keyword, and call that variable randomNumber
. You then use the nextInt()
method of the Random
class via the randomGenerator
instance you just created; use dot notation:
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt();
Then, Now limit the range of the randomNumber variable from 0 to 9. Use the version of nextInt() that takes a parameter specifying how many numbers to choose from. you need to restrict the range of the nextInt()
method by adding a 10 inside the brackets:
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(10);
Lastly, Create a String variable named intAsString
. Convert the randomNumber
integer to a String value and store it in the intAsString
variable. Hint: Add them together like in the video!. That looks like:
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(10);
String intAsString = randomNumber + "";
I hope that helps,
Steve.