0

I need to generate in a loop 200 numbers and signs that will create something like this: 1+1 or 20*1252 Is there a way to do this? I am new to c#. Can someone help?

Random randonNum = new Random();
for (int i = 0; i < 200; i++)
{
    int nums = randonNum.Next(0, 500); 
    //dont know how to continue
}
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
shir121
  • 13
  • 3
  • 8
    The question is not clear. `//dont know how to continue` Try to explain it in words and/or with pseudo code. – Tim Schmelter Feb 25 '15 at 10:45
  • 1
    It is not clear what you're trying to do. Do you want sums and multiplications? – U r s u s Feb 25 '15 at 10:45
  • So you're generating one number per iteration... now what do you want to do? It sounds like you might *at least* want to generate three numbers - two for the operands, and one to determine which operator to use. – Jon Skeet Feb 25 '15 at 10:45
  • First you must define the set of operators you want to randomly choose from, and the range of the numbers that you want to randomly generate (which looks like 0..499 from your existing code). – Matthew Watson Feb 25 '15 at 10:45
  • Do you also want to select an operator randomly? – Dave Feb 25 '15 at 10:45
  • maybe this can help you: http://www.dotnetperls.com/random – Dave Feb 25 '15 at 10:46
  • What do you want to do *next*? Is just printing the sums enough, or should a user provide an answer which you then need to check? In the latter case you must store the separate parts of the sum, instead of dumping it into a single string. – Hans Kesting Feb 25 '15 at 10:52

3 Answers3

2

You seem to know how to generate a random number, so generating two random numbers shouldn't prove any problem. It looks like you're having trouble generating a random "sign", called operators.

You can do so by creating an array containing the possible choices, generating a random index for that array and retrieving that element through the array's indexer as explained in Access random item in list:

var operators = new[] { "+", "-", "/", "*" };
int operatorIndex = randomNum.Next(operators.Length);
var selectedOperator = operators[operatorIndex];
Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • 1
    ok but `operator` is an illegal variable name and you forgot to rename `signs[...]` to `operators[...]` – xanatos Feb 25 '15 at 11:00
1

What about using Random for all three factors? Random can be used to find an operator from a list or array too for example.

You could use something like this (where operators is an array of string objects):

string[] operators = new string[] { "+", "-", "*" };
string oper = operators[randonNum.Next(operators.Length)];

Take a look at this working example:

// this list will contain the results
List<string> list = new List<string>();

// create an array of allowed operators
string[] operators = new string[] { "+", "-", "*" };

Random randonNum = new Random();
for (int i = 0; i < 200; i++)
{
    // pick two numbers
    int num1 = randonNum.Next(0, 500); 
    int num2 = randonNum.Next(0, 500); 

    // pick an operator from the array
    string oper = operators[randonNum.Next(operators.Length)];

    // add it to the list
    list.Add(string.Format("{0}{1}{2}", num1, oper, num2));
}
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
0

Something like this

      string[] operands = new[] {"+", "-", "*", "/"};
      Random random = new Random();
      List<string> results = new List<string>();
      for (int i = 0; i < 200; i++)
      {
        int firstNum = random.Next(0, 500);
        int secondNum = random.Next(0, 500);

        string operand = operands[random.Next(0, 3)];
        results.Add(string.Format("{0}{1}{2}", firstNum, operand, secondNum));
      }
zaitsman
  • 8,984
  • 6
  • 47
  • 79