What is the best way to generate a random addition problem?


What is the best way to generate a random addition problem in android on a textview using only numbers 1-9?

8 + 7 = ?
Computer GuyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Math.rand. No? Tried it?
Computer GuyAuthor Commented:
You want something like this:

Random r = new Random() ;
int first       = r.nextInt(9) + 1 ; // 1-9
int second = r.nextInt(9) + 1 ; // 1-9

String problem = Integer.toString(first) + " + " + Integer.toString(second) + " = ? " ;

Now show 'problem' in the text view.

Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Tomas Helgi JohannssonCommented:

To generate random number between 1 - 9 you can do this

Random rn = new Random();
int firstnum = rn.nextInt(9) + 1;
int secondnum = rn.nextInt(9) + 1;

Then simply present it using SetText method in TextView

I suggest you have a class that generates the math equation to solve and does the solving.

    Tomas Helgi

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Computer GuyAuthor Commented:
Also how can I generate the answer?

I have 3 buttons below the question I need to generate 2 random numbers and the other button needs to be the answer.
BTW I meant Math.random() - got my languages wrong. ;)
I need to generate 2 random numbers

All you need is

int r_1 = (int)(Math.random()*10);
int r_2 = (int)(Math.random()*10);
int answer = r_1+r_2;

Open in new window

Java.util.Random is for streams of numbers. Doesn't look like you need that.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.