Thursday 12 September 2013

Find a random integer which creates another integer, when using percentages

Find a random integer which creates another integer, when using percentages

I am sorry about the confusing title, let me clarify my problem:
My Java program is supposed to ask a math question with percentages.
It should create a question in this format:
25% of 4616 = ?
The requirements are:
the percentage is always dividable by 5 (no problem)
the number (here 4616) has to be between 100 and 9999 (also not a problem)
the number as well as the result has to be an integer (this is my problem)
Is there any fast method to find a random number which fulfills the last
requirement?
The only solution I can think of is to find the percentage, then create a
loop which will not stop until a random number is found which fulfills the
requirement (in the example until number % 4 == 0 is true)
But this loop could run thousands of times until a correct number is found.
Is there a better method for my problem?

No comments:

Post a Comment