0

I want to generate a set number of division questions. These questions must give an integer answer and not real number. Here's my code:

        int tempNum1, tempNum2; 
        do // Cannot give decimal answers for students
        {
            tempNum1 = numGen.Next(minValue, maxValue);
            tempNum2 = numGen.Next(minValue, maxValue);
        } while (tempNum1 % tempNum2 != 0);
        return String.Format("{0}/{1}", tempNum1, tempNum2);

Return values are stored in array, ready to be displayed. The problem is that generating takes too long; are there any solutions without needing to change the minV and maxV?

  • 6
    If you want to generate questions of type `a/b = c`, where all numbers are integers, don't randomly draw `a` and `b` but draw `b` and `c` then calculate `a = b * c`. – CompuChip Aug 25 '16 at 10:57
  • 1
    Wouldn't it be easier to take random 2 numbers, multiply them and then divide multiplication result on one of the arguments to get mod 0? – 3615 Aug 25 '16 at 10:58

1 Answers1

4

Why don't you generate the questions by multiplying?

int tempNum2 = numGen.Next(minValue, maxValue);
int f = numGen.Next(minValue, maxValue);
int tempNum1 = tempNum2 * f;
return String.Format("{0}/{1}", tempNum1, tempNum2);

Of course you would need to adjust maxValue for f to find numbers in the desired range.

René Vogt
  • 43,056
  • 14
  • 77
  • 99
  • btw: is this part of a homework? I'm sure some month ago there was an identical question...but can't find it anymore. – René Vogt Aug 25 '16 at 11:00