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

limiting the range of the random number variable

This quiz really has me stumped. Any help is appreciated. Thank you

RandomTest.java
Random randomGenerator = new Random(0);
int randomNumber = randomGenerator.nextInt();
String intAsString = ""+randomNumber;

Random randomGenerator = new Random(9);
int randomNumber = randomGenerator.nextInt();
String intAsString = ""+randomNumber;

1 Answer

Benjamin Barslev Nielsen
Benjamin Barslev Nielsen
18,958 Points

You should instantiate your Random instance without any arguments:

Random randomGenerator = new Random();

The nextInt method can take an int argument n, and then it returns an integer from and including 0 up to but not including n. This means that if we want a random number from 0-9 we can use the statement:

int randomNumber = randomGenerator.nextInt(10);

In this way we do not need a new Random instance each time we want a new range of the random number.