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 Generating a Random Number

Arcee Palabrica
Arcee Palabrica
8,100 Points

Code variations which one is more advisable to use...

I was just curious and tried using different code variations to generate 3 random numbers. How is it different from the one used in this video and which one is advisable to use?

Generating Random Numbers:

int randomNumber = (int) (Math.random() * 3);
// versus
int randomNumber = randomGenerator.nextInt(3);

Assign randomNumber to fact:

fact = Integer.toString(randomNumber);
//versus
fact = randomNumber + "";

I still got the same result when I ran the app.

2 Answers

Ben Deitch
STAFF
Ben Deitch
Treehouse Teacher

Hey Arcee! There's not always a clear best way to do something. Each of those code variations should work just fine; feel free to pick your favorite :)

Arcee Palabrica
Arcee Palabrica
8,100 Points

Ben Deitch thanks for the quick reply. I'm enjoying your videos. Have a great day.