Thread: Random math problems

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    25

    Random math problems

    How would you write a program to pick a math problem at random? i.e. add,divide,subtract,multiply? I guess using either subroutines or not but it would most likely be in a switch statment where the math problems are each a case and then another case would be random quesitons.....any suggestions or guidance?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That depends on how complicated you want your problems to be. Generally, you're going to have a random draw for each "element" of the problem. For example, if you wanted problems like (double-digit-number)(arithmetic-operator)(double-digit-number), you would need to generate each part. You would probably want to randomly choose the operator first, since that might set different limits on what you want your numbers to be.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    google the rand() function.

  4. #4
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    You'd want to think about what kind of distribution you're using. One might construct an algorithm like the following:

    Code:
    generateMathProblem(depth) {
        if (decideToContinue(depth)) {
            function = pickAFunction()
            for (1 .. function.arity()) {
                arguments.push_back(generateMathProblem(depth + 1))
            }
            return apply(function, arguments)
        }
        else {
            return randomConstant()
        }
    }
    
    newMathProblem() {
        return generateMathProblem(depth)
    }
    Then you'd tweak the probability of decideToContinue returning true for the various depths to control how big you want your math problems to be.
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Math problems...
    By Chaplin27 in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 06-14-2005, 07:46 PM
  2. Question for Random class
    By joenching in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2005, 11:22 PM
  3. Problems implementing quicksort function
    By rvanbeusichem in forum C Programming
    Replies: 3
    Last Post: 11-24-2004, 09:00 PM
  4. Problems with Random Numbers
    By kinghajj in forum C Programming
    Replies: 2
    Last Post: 04-15-2004, 03:44 PM
  5. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM