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 (retired 2014) Learning the Language Objects and Random Numbers

habib kazemi
habib kazemi
2,041 Points

I can't understand this code

I down't know what is Random() ? is this a method Random randomGenetor = new Random(); If Yes so there is not method type, ... .

2 Answers

Matheus G Oliveira
Matheus G Oliveira
9,682 Points

Random is a method that generates random items... for example:

you have an array arrayList ={"sunday","monday", "tuesday", "wednesday"} and you want to have random days, you use this to generate it

by the way, having Random randomGenerator = new Random()

is a constructor you name Random as randomGenerator and create a new object using the constructor Random()

habib kazemi
habib kazemi
2,041 Points

And what about this code can you explain it.

int randomnumber = randomGenerator.nextInt(3);
Matheus G Oliveira
Matheus G Oliveira
9,682 Points

That means that you are setting the maximum random numbers to 3.

On this case you will get a result between 0, 1, 2. It means that it will randomize 3 numbers, from 0 to 2.

If you set the nextInt(10), you will get a result between 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

Got it?