1

So I'm building a small math game where random sums are generated and, upon being answered, added to either a right or wrong score. It's going well, and I've received some help with certain things and learnt along the way, but I've hit another problem I can't figure out. The game picks between +, -, * and / operators when generating sums, and +, - and * work well, but / often calls for a decimal answer which the program does not like. I'd like to try to figure out a way to make it not generate a number to divide by that would result in a decimal answer when diving the first number. Here's some example code to clear what I have so far up:

        var randomNum = new Random();
        num1 = randomNum.Next(0, 10);
        num2 = randomNum.Next(0, 10);
        char[] operators = { '+', '-', '*', '/' };
        char op = operators[randomNum.Next(operators.Length)];
        switch (op)
        {
            case '+':
                answer = num1 + num2;
                label1.Text = num1.ToString() + " + " + num2.ToString() + " = ";
                break;

            case '-':
                answer = num1 - num2;
                label1.Text = num1.ToString() + " - " + num2.ToString() + " = ";
                break;

            case '*':
                answer = num1 * num2;
                label1.Text = num1.ToString() + " * " + num2.ToString() + " = ";
                break;

            case '/':
                answer = num1 / num2;
                label1.Text = num1.ToString() + " / " + num2.ToString() + " = ";
                break;
         }

I've tried moving the bits that state what num1 and num2 are into each of the cases, so that they look like this:

            case '/':
                num1 = randomNum.Next(0, 10);
                num2 = randomNum.Next(0, 10);
                answer = num1 / num2;
                label1.Text = num1.ToString() + " / " + num2.ToString() + " = ";
                break;

But I can't conceive of what I could put in the brackets instead of having (0, 10) to avoid decimal sum answers. Is there a way I can have it determine if an answer will be a decimal one, and if it is re-roll num2 to try and get a whole number answer? Thanks!

Mango
  • 91
  • 2
  • 10

2 Answers2

1

Because integer division rounds down, you should change num1 to ensure an exact division:

num1 = randomNum.Next(0, 10);
num2 = randomNum.Next(1, 10);//Cannot divide by 0!!
answer = num1 / num2;
num1 = answer * num2;

if num1 = 7 and num2 = 3, answer will be 2 and num1 is changed to 6; making num1 == 6, num2 == 3 and answer == 2

You could also generate num2 and answer and calculate num1

num2 = randomNum.Next(1, 10);//Cannot divide by 0!!
answer = randomNum.Next(0, 10);
num1 = answer * num2; // answer == num1 / num2
Ahmed KRAIEM
  • 10,267
  • 4
  • 30
  • 33
  • I didn't know that about integers, but now that I do that makes everything easier. Also, everything you'd demonstrated here makes sense and works. Thanks! :) – Mango Sep 09 '13 at 14:34
1

Pretty simple. Send in the whole number, you'll get out a number that will divide evenly (easy for a kid).

public static class TestWholeNumber
{
    public static int ReturnWholeNumber(int testnum)
    {
        var randomNum = new Random(Guid.NewGuid().GetHashCode());

        int num2 = 0;
        do
        {
            num2 = randomNum.Next(1, 10);
        }
        while (testnum % num2 != 0);
        return num2;
    }
}
Slack Shot
  • 1,090
  • 6
  • 10
  • 1
    You will also want to seed your random class instance.. or the questions and answers will get pretty boring. – Slack Shot Sep 09 '13 at 13:50
  • Can you tell me a bit more about this piece of code you've demonstrated? What exactly is it doing and how? And what do you mean by 'seeding my random class instance'? – Mango Sep 09 '13 at 14:33
  • 1
    In the Do while loop, it is testing to find a number that evenly divides the submitted number. The "%" operator is the modulus operator in C#. The reason I mentioned the seeding.. is because if you test your console out.. for generated numbers.. you'll see they kind of repeat a lot, with your current code, right? By putting a seed in your "new Random(Guid.NewGuid().GetHashCode());" it creates more varied random numbers. – Slack Shot Sep 09 '13 at 14:42
  • Okay, this seems like a good method too then... But if I were to take up your suggestion about adding a seed, how would I go about that? – Mango Sep 09 '13 at 14:50
  • 1
    On your first line of your submitted code.. Change " var randomNum = new Random();" to my code.. "var randomNum = new Random(Guid.NewGuid().GetHashCode());" and thanks for the vote up. Need all the rep I can get. : ) – Slack Shot Sep 09 '13 at 14:52
  • You're welcome :P And is adding that all I need to do? Where does the seed come from? – Mango Sep 09 '13 at 15:21
  • 1
    The seed is coming from a new instance of GUID.. which is pretty dang random.. then it is getting that object's hash code which is an integer. The hashcode is used in comparing equality to other objects of the same type. – Slack Shot Sep 09 '13 at 15:23
  • So why is this more random than no seed at all? How is the way it calculates things different between having a seed and no seed? – Mango Sep 09 '13 at 15:24
  • 1
    http://stackoverflow.com/questions/1785744/how-do-i-seed-a-random-class-to-avoid-getting-duplicate-random-values Better to re-use answers than to make one up myself. : ) – Slack Shot Sep 09 '13 at 15:25
  • Both what you just said and what the page you linked to said made sense. Well thanks! I've used Ahmed's solution to the initial question this page exists for, but I've taken you up on the seed suggestion :) – Mango Sep 09 '13 at 15:31
  • 1
    His is technically lower overhead.. so I agree with the decision. Mine was posted after his.. Guess I was making that code while he had already posted his answer. ^.^ – Slack Shot Sep 09 '13 at 15:36