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 (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?