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 trial

Android Build a Simple Android App Basic Android Programming Using the Random Class

Josephine Dinesh
Josephine Dinesh
476 Points

initialize a new Random Variable called randomGenerator

initialize a new Random Variable called randomGenerator Hint:Don't forget to add new keyword

RandomTest.java
Random randomGenerator;

The solution here is to set it equal to new Random().

Random randomGenerator = new Random();

Hope this helps :).

1 Answer

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Josephine,

like Paul already said. Use this line of code to create a new Random object.

Random randomGenerator = new Random();

On the left side you see whats called variable declaration. You are creating a variable called "randomGenerator" and its type is Random. On the right side you are creating a new Object of a Random class using the keyword "new". The equals sign combines them together, so your new variable can use methods and field members from the Random class.

If you are asking yourself whats a class ... it is a blueprint where methods and different other variables are defined. The Random class has the nextInt() method you are using to generate a new integer. So to be able to use this methods, you create an Object of the Random class.

I hope that crears things up ....

Let us know if you need more information.

Grigorij