Android Build a Simple Android App Basic Android Programming Generating a Random Number

Arcee Palabrica
Arcee Palabrica
8,099 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,099 Points

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