1

I am trying to make a random Trinomial generator and I want the 2 random numbers to follow the trinomial rules (num1+num2=b)(num1*num2=c)

string a = "x²";
int b = new Random().Next(-50, 50);
int c = new Random().Next(-50, 50);
Console.WriteLine(a,b,c);
while (true)
{
  int num1 = int.Parse(Console.ReadLine());
  int num2 = int.Parse(Console.ReadLine());
  if ((num1 + num2 == b) && (num1 * num2 == c))
  {
    Console.WriteLine("Correct.");
    break;
  }
  else
  {
    Console.WriteLine("Wrong. Try again");
  }
}

I expect the numbers to be written down but they aren't. Also, I don't know how to make the random numbers follow these rules. PS - The random numbers are always the same, how do I change that?

2 Answers2

1

Here's my attempt at Charles' suggestion:

var rand = new Random();
string a = "x²";
int num1 = rand.Next(-50, 50);
int num2 = rand.Next(-50, 50);

int b = num1 + num2;
int c = num1 * num2;

Console.WriteLine($"{a}, {b}, {c}");
while (true)
{
    int guess1 = int.Parse(Console.ReadLine());
    int guess2 = int.Parse(Console.ReadLine());
    if (guess1 == num1 && guess2 == num2)
    {
        break;
    }

    Console.WriteLine("Wrong. Try again");
}

Console.WriteLine("Correct.");

I've simplified the logic at the end a bit, but it should work the same.

Robin Bennett
  • 3,192
  • 1
  • 8
  • 18
  • 2
    I'd note you need to share the instance of `Random` or you'll get the same two numbers every time. See [this question](https://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number/768001) for more info. Also, that overload of `Console.WriteLine` would only print the value of `a` (believing it to be a format string + args). Perhaps `Console.WriteLine($"x² + {b}x + {c}")` might be better? – Charles Mager Feb 08 '19 at 12:11
  • 1
    Thanks, I really should have tried it before posting it! I've changed the code. I left the writeline using commas, as sometimes the values have minus signs. Also the inputs need to be checked to ensure they're numbers, but didn't want to make the code longer and more complex than required to demonstrate your idea. – Robin Bennett Feb 08 '19 at 13:04
1

Try this:

string a = "x²";
var randomGenerator = new Random();
int b = randomGenerator.Next(-50, 50);
int c = randomGenerator.Next(-50, 50);
Console.WriteLine("{0},{1},{2}", a, b, c);

bool isRunning = true;
while (isRunning)
{
    int num1 = int.Parse(Console.ReadLine());
    int num2 = int.Parse(Console.ReadLine());
    if ((num1 + num2 == b) && (num1 * num2 == c))
    {
        Console.WriteLine("Correct.");
        isRunning = false;
    }
    else
    {
        Console.WriteLine("Wrong. Try again");
    }
}
Console.ReadLine();

Explanation:

First of all the Random problem. Random generates numbers not really in a random way but calculates them. So since it is an algorithm it would work the same every try. To counter that, random seeds itself with the current time which then changes the output of the algorithm. In your case you create 2 random objects, but they will be generated so fast, that both actually seed with the same time, therefore calculating the same "random" numbers. That's why in my solution, we only create one Random object.

Second: If you just want to write one string to the console, jus concat the string and pass it as one parameter.

cmos
  • 482
  • 4
  • 14
  • Thanks, I see how it works now and thanks for explaining but whenever I try to run it it says that $ is an unexpected character – Yotam Fogel Feb 08 '19 at 13:22