I tried this for my math quiz and it works wonders, but I did not get the fractional part:
public void gameContinue() {
int num1 = (int) (Math.random() * 100 1);
java.util.List<Integer> firstNumDivisors = getDivisors(num1);
int divisorsRandIndex = (int) (Math.random() * firstNumDivisors.size());
int num2 = firstNumDivisors.get(divisorsRandIndex);
question.setText(num1 " ÷ " num2);
// Create your question from first and second numbers here
}
private static java.util.List<Integer> getDivisors (int number) {
java.util.List<Integer> divisors = new java.util.ArrayList<>();
for (int i = 1; i <= number; i )
if (number % i == 0)
divisors.add(i);
return divisors;
}
But after some testing, I do not want to include prime numbers (more than 10 like 11, 13, 17, 19 and so on) and I also do not want the answer to be more than 10 (11, 12, 13 and so on)
Should I make a string for prime numbers and make the program exclude those? Also, how can I make the answer to be no more than 10?
Thank you
CodePudding user response:
(Some points of your question required some interpretation - hopefully I was correct; otherwise, just add a comment and I'll try to adapt.)
Require answer always <= 10
To ensure the selected divisor does not result in an "answer" more than 10, then simply restrict the divisors added to the divisor list.
Modify the one line in getDivisors
as such:
// i is an integral divisor and the resulting quotient is <= 10
if (number % i == 0 && number / i <= 10)
Then when the code randomly selects a divisor from the list, the "answer" to the question:
num1 ÷ num2
will always be <= 10, where num1 is a number from 1 to 100 (inclusive) and num2 is the randomly selected integral divisor.
Exclude some dividends (num1
)
In order to exclude certain numbers from a range (for your num1
) you could do it as a loop as in here.
Or you could create a list of the acceptable numbers and then use Random
to provide an index into your acceptable list (credit reference answer for list creation):
// an array of acceptable numbers as dividend
int[] acceptableNums = IntStream.range(1, 101).filter(a -> !java.util.List.of(11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97).contains(a)).toArray();
// here i chose to use "r" which is java.util.Random r = new java.util.Random() because my test involved a loop and I want to get uniform distribution over the loop.
int num1 = acceptableNums[r.nextInt(acceptableNums.length)];
So the complete test:
public static void main(String[] args) {
java.util.Random r = new java.util.Random();
int[] acceptableNums = IntStream.range(1, 101).filter(a -> !java.util.List.of(11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97).contains(a)).toArray();
for (int i = 0; i < 100; i ) {
int num1 = acceptableNums[r.nextInt(acceptableNums.length)];
java.util.List<Integer> firstNumDivisors = getDivisors(num1);
int divisorsRandIndex = (int) (Math.random() * firstNumDivisors.size());
int num2 = firstNumDivisors.get(divisorsRandIndex);
System.out.println("What is " num1 " ÷ " num2 " (Answer: " num1/num2 ")");
// Create your question from first and second numbers here
}
}
private static java.util.List<Integer> getDivisors ( int number){
java.util.List<Integer> divisors = new java.util.ArrayList<>();
for (int i = 1; i <= number; i )
if (number % i == 0 && number / i <= 10)
divisors.add(i);
return divisors;
}
And the output:
What is 78 ÷ 39 (Answer: 2)
What is 1 ÷ 1 (Answer: 1)
What is 32 ÷ 8 (Answer: 4)
What is 68 ÷ 17 (Answer: 4)
What is 26 ÷ 13 (Answer: 2)
What is 94 ÷ 47 (Answer: 2)
What is 46 ÷ 23 (Answer: 2)
What is 68 ÷ 68 (Answer: 1)
What is 10 ÷ 2 (Answer: 5)
What is 70 ÷ 10 (Answer: 7)
What is 38 ÷ 38 (Answer: 1)
What is 75 ÷ 75 (Answer: 1)
What is 1 ÷ 1 (Answer: 1)
What is 90 ÷ 30 (Answer: 3)
What is 62 ÷ 62 (Answer: 1)
What is 95 ÷ 95 (Answer: 1)
What is 60 ÷ 6 (Answer: 10)
What is 27 ÷ 3 (Answer: 9)
What is 5 ÷ 1 (Answer: 5)
What is 28 ÷ 14 (Answer: 2)
What is 1 ÷ 1 (Answer: 1)
What is 70 ÷ 14 (Answer: 5)
What is 21 ÷ 21 (Answer: 1)
What is 40 ÷ 40 (Answer: 1)
What is 51 ÷ 17 (Answer: 3)
What is 24 ÷ 12 (Answer: 2)
What is 5 ÷ 1 (Answer: 5)
What is 58 ÷ 58 (Answer: 1)
What is 91 ÷ 91 (Answer: 1)
What is 91 ÷ 13 (Answer: 7)
What is 94 ÷ 94 (Answer: 1)
What is 28 ÷ 14 (Answer: 2)
What is 46 ÷ 23 (Answer: 2)
What is 95 ÷ 95 (Answer: 1)
What is 58 ÷ 58 (Answer: 1)
What is 2 ÷ 2 (Answer: 1)
What is 55 ÷ 55 (Answer: 1)
What is 20 ÷ 5 (Answer: 4)
What is 92 ÷ 23 (Answer: 4)
What is 21 ÷ 21 (Answer: 1)
What is 10 ÷ 5 (Answer: 2)
What is 1 ÷ 1 (Answer: 1)
What is 69 ÷ 69 (Answer: 1)
What is 84 ÷ 21 (Answer: 4)
What is 96 ÷ 24 (Answer: 4)
What is 90 ÷ 45 (Answer: 2)
What is 88 ÷ 44 (Answer: 2)
What is 88 ÷ 22 (Answer: 4)
What is 72 ÷ 36 (Answer: 2)
What is 40 ÷ 5 (Answer: 8)
What is 8 ÷ 4 (Answer: 2)
What is 10 ÷ 2 (Answer: 5)
What is 24 ÷ 3 (Answer: 8)
What is 66 ÷ 66 (Answer: 1)
What is 90 ÷ 30 (Answer: 3)
What is 66 ÷ 66 (Answer: 1)
What is 58 ÷ 58 (Answer: 1)
What is 91 ÷ 91 (Answer: 1)
What is 58 ÷ 58 (Answer: 1)
What is 28 ÷ 7 (Answer: 4)
What is 96 ÷ 16 (Answer: 6)
What is 90 ÷ 30 (Answer: 3)
What is 91 ÷ 91 (Answer: 1)
What is 12 ÷ 4 (Answer: 3)
What is 4 ÷ 4 (Answer: 1)
What is 84 ÷ 84 (Answer: 1)
What is 8 ÷ 1 (Answer: 8)
What is 49 ÷ 7 (Answer: 7)
What is 14 ÷ 7 (Answer: 2)
What is 16 ÷ 8 (Answer: 2)
What is 51 ÷ 17 (Answer: 3)
Inverse Approach
Given the comment below clarifying the desired result, the inverse approach to the equation is simpler and has the benefit of naturally eliminating the primes > 10. (A number 1..10 multiplied by a number 1..10 cannot result in a prime > 10.)
java.util.Random r = new java.util.Random();
for (int i = 0; i < 100; i ) {
int num2 = r.nextInt(10) 1;
int answer = r.nextInt(10) 1;
int num1 = num2 * answer;
System.out.println("What is " num1 " ÷ " num2 " (Answer: " answer ")");
// Create your question from first and second numbers here
}
And result from inverse approach:
What is 21 ÷ 7 (Answer: 3)
What is 4 ÷ 1 (Answer: 4)
What is 100 ÷ 10 (Answer: 10)
What is 40 ÷ 4 (Answer: 10)
What is 64 ÷ 8 (Answer: 8)
What is 16 ÷ 8 (Answer: 2)
What is 14 ÷ 2 (Answer: 7)
What is 63 ÷ 7 (Answer: 9)
What is 56 ÷ 7 (Answer: 8)
What is 4 ÷ 2 (Answer: 2)
What is 10 ÷ 2 (Answer: 5)
What is 18 ÷ 6 (Answer: 3)
What is 5 ÷ 5 (Answer: 1)
What is 42 ÷ 7 (Answer: 6)
What is 5 ÷ 1 (Answer: 5)
What is 54 ÷ 6 (Answer: 9)
What is 14 ÷ 2 (Answer: 7)
What is 14 ÷ 2 (Answer: 7)
What is 50 ÷ 5 (Answer: 10)
What is 36 ÷ 6 (Answer: 6)
What is 12 ÷ 6 (Answer: 2)
What is 56 ÷ 8 (Answer: 7)
What is 24 ÷ 8 (Answer: 3)
What is 12 ÷ 4 (Answer: 3)
What is 20 ÷ 5 (Answer: 4)
What is 100 ÷ 10 (Answer: 10)
What is 36 ÷ 4 (Answer: 9)
What is 18 ÷ 9 (Answer: 2)
What is 48 ÷ 6 (Answer: 8)
What is 40 ÷ 4 (Answer: 10)
What is 42 ÷ 6 (Answer: 7)
What is 8 ÷ 2 (Answer: 4)
What is 18 ÷ 2 (Answer: 9)
What is 18 ÷ 3 (Answer: 6)
What is 70 ÷ 10 (Answer: 7)
What is 8 ÷ 1 (Answer: 8)
What is 12 ÷ 3 (Answer: 4)
What is 24 ÷ 6 (Answer: 4)
What is 8 ÷ 4 (Answer: 2)
What is 6 ÷ 3 (Answer: 2)
What is 20 ÷ 10 (Answer: 2)
What is 30 ÷ 6 (Answer: 5)
What is 70 ÷ 10 (Answer: 7)
What is 42 ÷ 6 (Answer: 7)
What is 6 ÷ 2 (Answer: 3)
What is 15 ÷ 3 (Answer: 5)
What is 12 ÷ 3 (Answer: 4)
What is 10 ÷ 2 (Answer: 5)
What is 70 ÷ 7 (Answer: 10)
What is 6 ÷ 1 (Answer: 6)
What is 80 ÷ 8 (Answer: 10)
What is 7 ÷ 1 (Answer: 7)
What is 45 ÷ 5 (Answer: 9)
What is 21 ÷ 7 (Answer: 3)
What is 48 ÷ 6 (Answer: 8)
What is 12 ÷ 3 (Answer: 4)
What is 21 ÷ 3 (Answer: 7)
What is 48 ÷ 8 (Answer: 6)
What is 80 ÷ 8 (Answer: 10)
What is 50 ÷ 5 (Answer: 10)
What is 18 ÷ 3 (Answer: 6)
What is 24 ÷ 8 (Answer: 3)
What is 30 ÷ 5 (Answer: 6)
What is 54 ÷ 9 (Answer: 6)
What is 8 ÷ 8 (Answer: 1)
What is 8 ÷ 8 (Answer: 1)
What is 40 ÷ 8 (Answer: 5)
What is 6 ÷ 1 (Answer: 6)
What is 60 ÷ 10 (Answer: 6)
What is 60 ÷ 6 (Answer: 10)
What is 45 ÷ 9 (Answer: 5)
What is 48 ÷ 6 (Answer: 8)
What is 50 ÷ 5 (Answer: 10)
What is 24 ÷ 3 (Answer: 8)
What is 6 ÷ 3 (Answer: 2)
What is 5 ÷ 1 (Answer: 5)
What is 4 ÷ 1 (Answer: 4)
What is 20 ÷ 4 (Answer: 5)
What is 9 ÷ 3 (Answer: 3)
What is 45 ÷ 5 (Answer: 9)
What is 3 ÷ 1 (Answer: 3)
What is 6 ÷ 2 (Answer: 3)
What is 54 ÷ 6 (Answer: 9)
What is 21 ÷ 3 (Answer: 7)
What is 50 ÷ 10 (Answer: 5)
What is 20 ÷ 5 (Answer: 4)
What is 6 ÷ 3 (Answer: 2)
What is 10 ÷ 10 (Answer: 1)
What is 60 ÷ 6 (Answer: 10)
What is 10 ÷ 10 (Answer: 1)
What is 72 ÷ 9 (Answer: 8)
What is 15 ÷ 5 (Answer: 3)
What is 16 ÷ 8 (Answer: 2)
What is 70 ÷ 10 (Answer: 7)
What is 24 ÷ 4 (Answer: 6)
What is 16 ÷ 8 (Answer: 2)
What is 80 ÷ 10 (Answer: 8)
What is 40 ÷ 10 (Answer: 4)
What is 12 ÷ 6 (Answer: 2)
What is 16 ÷ 2 (Answer: 8)