Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,363 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