# Building a simple random equation of size

I'm trying to find a way to make this code more efficient than using a lot of if statements because there are a lot of combinations. The final result variable is an equation made from randomly chosen arithmetic signs and the operands are random numbers between 1 and 10 to create an equation. But after 2 operands there are a lot of combinations so I need help finding a different way so I don't have to manually put in all the possible combinations in if statements. The equations can either have two operators at the least to 4 operators and up to to 5 operands per equation so each equation will be randomly created.

public class MathEngine : MonoBehaviour## Heading ##
{
public Text equationText;
public bool setFixedNumOfOperands;

public bool sumOnly;
public bool subtractionOnly;
public bool multiplyOnly;
public bool divideOnly;

private List<string> randomSigns;
private List<int> operandList;

[Range(2,5)]
public int numberOFOperands;

public int operandRange;

public int operandRangeMinimum = 1;
public int operandRangeMaximum= 10;

string randomsigns;

private float finalResult;

private float operand1;
private float operand2;
private float operand3;
private float operand4;
private float operand5;

// Use this for initialization
private void Start()
{
operandRange = Random.Range(operandRangeMinimum, operandRangeMaximum + 1);

operand1 = Random.Range(operandRangeMinimum, operandRangeMaximum + 1);
operand2 = Random.Range(operandRangeMinimum, operandRangeMaximum + 1);
operand3 = Random.Range(operandRangeMinimum, operandRangeMaximum + 1);
operand4 = Random.Range(operandRangeMinimum, operandRangeMaximum + 1);
operand5 = Random.Range(operandRangeMinimum, operandRangeMaximum + 1);

operandList = new List<int>();
randomSigns = new List<string>();

string sign1 = randomSigns[Random.Range(0, 4)];
string sign2 = randomSigns[Random.Range(0,4)];
string sign3 = randomSigns[Random.Range(0, 4)];
string sign4 = randomSigns[Random.Range(0, 4)];
string sign5 = randomSigns[Random.Range(0, 4)];

string sign =  randomSigns[Random.Range(0, 4)];

if (setFixedNumOfOperands == false)
{
numberOFOperands = Random.Range(2, 6);

for (int i = 0; i < numberOFOperands; i++)
{
//randomsigns = randomSigns[Random.Range(0, 4)];
}
Debug.Log("random: " + numberOFOperands);
Debug.Log("number of operands in list: " + operandList.Count);

}
else
{
for (int i = 0; i < numberOFOperands; i++)
{
}
Debug.Log("number of operands in list: " + operandList.Count);

}

//----------2 sign combinations 3 operands--------
if (numberOFOperands == 3 && setFixedNumOfOperands)
{
//----If all signs are the same-----
if (sign1 == "+" && sign2 == "+")
{
finalResult = operand1 + operand2 + operand3;
}
if (sign1 == "-" && sign2 == "-")
{
finalResult = operand1 - operand2 - operand3;
}
if (sign1 == "*" && sign2 == "*")
{
finalResult = operand1 * operand2 * operand3;
}

if (sign1 == "/" && sign2 == "/")
{
finalResult = operand1 / operand2 / operand3;
}

if (sign1 == "+" && sign2 == "/")
{
finalResult = (operand1 + operand2) / operand3;
}
if (sign1 == "+" && sign2 == "-")
{
finalResult = operand1 + operand2 - operand3;
}
if (sign1 == "+" && sign2 == "*")
{
finalResult = operand1 + (operand2 * operand3);
}

//---------Subtraction Combinations---------
if (sign1 == "-" && sign2 == "/")
{
finalResult = (operand1 - operand2) / operand3;
}
if (sign1 == "-" && sign2 == "+")
{
finalResult = operand1 - operand2 + operand3;
}
if (sign1 == "-" && sign2 == "*")
{
finalResult = operand1 - (operand2 * operand3);
}
//---------Multiplication Combinations---------
if (sign1 == "*" && sign2 == "/")
{
finalResult = (operand1 * operand2) / operand3;
}
if (sign1 == "*" && sign2 == "+")
{
finalResult = (operand1 * operand2) + operand3;
}
if (sign1 == "*" && sign2 == "-")
{
finalResult = (operand1 * operand2) - operand3;
}
//---------Division Combinations---------
if (sign1 == "/" && sign2 == "*")
{
finalResult = (operand1 / operand2) * operand3;
}
if (sign1 == "/" && sign2 == "+")
{
finalResult = (operand1 / operand2) + operand3;
}
if (sign1 == "/" && sign2 == "-")
{
finalResult = (operand1 / operand2) - operand3;
}

//--------if only 2 signs and 3 operands -------------
equationText.text = operand1 + " " + sign1 + " " + operand2 + " " + sign2 + " " + operand3 + " " + "= " + finalResult;
}

//----------3 sign combinations 4 operands--------
if (numberOFOperands == 4 && setFixedNumOfOperands)
{
//----If all signs are the same-----
if (sign1 == "+" && sign2 == "+" && sign3 == "+")
{
finalResult = operand1 + operand2 + operand3 + operand4;
}
if (sign1 == "-" && sign2 == "-" && sign3 == "-")
{
finalResult = operand1 - operand2 - operand3 - operand4;
}
if (sign1 == "*" && sign2 == "*" && sign3 == "*")
{
finalResult = operand1 * operand2 * operand3 * operand4;
}

if (sign1 == "/" && sign2 == "/" && sign3 == "/")
{
finalResult = operand1 / operand2 / operand3 / operand4;
}

if (sign1 == "+" && sign2 == "/" && sign3 == "/")
{
finalResult = (operand1 + operand2) / operand3 / operand4;
}
if (sign1 == "+" && sign2 == "-" && sign3 == "-")
{
finalResult = operand1 + operand2 - operand3 - operand4;
}
if (sign1 == "+" && sign2 == "*" && sign3 == "*" )
{
finalResult = operand1 + (operand2 * operand3) * operand4;
}
//---------
if (sign1 == "+" && sign2 == "+" && sign3 == "*")
{
finalResult = operand1 + operand2 + (operand3 * operand4);
}
if (sign1 == "+" && sign2 == "+" && sign3 == "/")
{
finalResult = operand1 + operand2 + (operand3 / operand4);
}
if (sign1 == "+" && sign2 == "+" && sign3 == "-")
{
finalResult = operand1 + operand2 + operand3 - operand4;
}
//-----------

if (sign1 == "+" && sign2 == "*" && sign3 == "+")
{
finalResult = operand1 + (operand2 * operand3) + operand4;
}
if (sign1 == "+" && sign2 == "/" && sign3 == "+")
{
finalResult = operand1 + (operand2 / operand3) + operand4;
}
if (sign1 == "+" && sign2 == "-" && sign3 == "+")
{
finalResult = operand1 + operand2 - operand3 + operand4;
}

//---------Subtraction Combinations---------
if (sign1 == "-" && sign2 == "/" && sign3 == "/")
{
finalResult = (operand1 - operand2) / (operand3 / operand4);
}
if (sign1 == "-" && sign2 == "+" && sign3 == "+")
{
finalResult = operand1 - operand2 + operand3 + operand4;
}
if (sign1 == "-" && sign2 == "*" && sign3 == "*")
{
finalResult = operand1 - (operand2 * operand3) * operand4;
}
//---------
if (sign1 == "-" && sign2 == "-" && sign3 == "*")
{
finalResult = operand1 - operand2 - (operand3 * operand4);
}
if (sign1 == "-" && sign2 == "-" && sign3 == "/")
{
finalResult = operand1 - operand2 - (operand3 / operand4);
}
if (sign1 == "-" && sign2 == "-" && sign3 == "+")
{
finalResult = operand1 - operand2 - operand3 + operand4;
}
//-----------

if (sign1 == "-" && sign2 == "*" && sign3 == "-")
{
finalResult = operand1 - (operand2 * operand3) - operand4;
}
if (sign1 == "-" && sign2 == "/" && sign3 == "-")
{
finalResult = operand1 - (operand2 / operand3) - operand4;
}
if (sign1 == "-" && sign2 == "+" && sign3 == "-")
{
finalResult = operand1 - operand2 + operand3 - operand4;
}

//---------Multiplication Combinations---------
if (sign1 == "*" && sign2 == "/" && sign3 == "/")
{
finalResult = (operand1 * operand2) / operand3 / operand4;
}
if (sign1 == "*" && sign2 == "+" && sign3 == "+")
{
finalResult = (operand1 * operand2) + operand3 + operand4;
}
if (sign1 == "*" && sign2 == "-" && sign3 == "-")
{
finalResult = (operand1 * operand2) - operand3 - operand4;
}
//---------
if (sign1 == "*" && sign2 == "*" && sign3 == "-")
{
finalResult = (operand1 * operand2) * operand3 - operand4;
}
if (sign1 == "*" && sign2 == "*" && sign3 == "/")
{
finalResult = (operand1 * operand2) * (operand3 / operand4);
}
if (sign1 == "*" && sign2 == "*" && sign3 == "+")
{
finalResult = (operand1 * operand2) *  (operand3 + operand4);
}
//-----------

if (sign1 == "*" && sign2 == "-" && sign3 == "*")
{
finalResult = (operand1 * operand2) - (operand3 * operand4);
}
if (sign1 == "*" && sign2 == "/" && sign3 == "*")
{
finalResult = (operand1 * operand2) / (operand3 * operand4);
}
if (sign1 == "*" && sign2 == "+" && sign3 == "*")
{
finalResult = (operand1 * operand2) + (operand3 * operand4);
}

//---------Division Combinations---------
if (sign1 == "/" && sign2 == "*" && sign3 == "*")
{
finalResult = (operand1 / operand2) * operand3 * operand4;
}
if (sign1 == "/" && sign2 == "+" && sign3 == "+")
{
finalResult = (operand1 / operand2) + operand3 + operand4;
}
if (sign1 == "/" && sign2 == "-" && sign3 == "-")
{
finalResult = (operand1 / operand2) - operand3 - operand4;
}

//---------
if (sign1 == "/" && sign2 == "/" && sign3 == "-")
{
finalResult = (operand1 / operand2) / (operand3 - operand4);
}
if (sign1 == "/" && sign2 == "/" && sign3 == "*")
{
finalResult = (operand1 / operand2) / (operand3 * operand4);
}
if (sign1 == "/" && sign2 == "/" && sign3 == "+")
{
finalResult = (operand1 / operand2) / (operand3 + operand4);
}
//-----------

if (sign1 == "/" && sign2 == "-" && sign3 == "/")
{
finalResult = (operand1 / operand2) - (operand3 / operand4);
}
if (sign1 == "/" && sign2 == "*" && sign3 == "/")
{
finalResult = (operand1 / operand2) * (operand3 / operand4);
}
if (sign1 == "/" && sign2 == "+" && sign3 == "/")
{
finalResult = (operand1 / operand2) + (operand3 / operand4);
}

//--------if only 3 signs and 4 operands -------------
equationText.text = operand1 + " " + sign1 + " " + operand2 + " " + sign2 + " " + operand3 +" " + sign3 + " " + operand4 + " " + "= " + finalResult;
}

}

• What is Random.Range? Is this something you wrote yourself or is this a unity API? Commented Jul 1, 2017 at 17:59
• Random.range is unity API Commented Jul 1, 2017 at 19:31
• I think the algorithm is simply bad. Make two stacks, one of n operands and one and one of n-1 random operators. pop operands and operators, apply the math, push the result back on the operand stack. continue until the operator stack is empty. This is a 20 line program. Commented Jul 1, 2017 at 22:10
• Whenever you see that much repetition, there's something wrong. Commented Jul 1, 2017 at 22:47
• @TonyEnnis Would you care to post your 20 line solution? Commented Jul 3, 2017 at 20:02

What is the purpose of operandList? All you use is the count and that is numberOFOperands.

Just take two passes and do / * in the first pass.

Use a List rather than operand1 - operand5

Use a List rather than sign1 - sign5

I'm not sure this is correct but something like this.

public static float Maths2(out string equation)
{
Random rand = new Random();
List<char> operators = new List<char>() { '+', '-', '/', '*' };
List<int> operatorsThis = new List<int>();
List<float> operandsThis = new List<float>();
int operatorsCount = 4;
int operandsMin = 1;
int operandsMax = 10;
StringBuilder sb = new StringBuilder();
float f;
List<int> operatorsThisPlusMinus = new List<int>();
List<float> operandsPlusMinus = new List<float>();

for (int i = 0; i < operatorsCount; i++)
{
f = rand.Next(operandsMin, operandsMax + 1);
sb.Append(f.ToString() + " ");

int r = rand.Next(4);
sb.Append(operators[r] + " ");
}
f = rand.Next(operandsMin, operandsMax + 1);
sb.Append(f.ToString() + " ");
equation = sb.ToString();
Debug.WriteLine(equation);
f = operandsThis[0];
for (int i = 0; i < operatorsThis.Count; i++)
{
if (operatorsThis[i] < 2)
{
f = operandsThis[i + 1];
}
else if (operatorsThis[i] == 2)
{
f /= operandsThis[i + 1];
}
else if (operatorsThis[i] == 3)
{
f *= operandsThis[i + 1];
}
}
for (int i = 0; i < operatorsThisPlusMinus.Count; i++)
{
if (operatorsThisPlusMinus[i] == 0)
{
}
else if (operatorsThisPlusMinus[i] == 1)
{
}
}
}


Ok here I got it to multiply and divide with one operator but the result is still wrong when there are more than one operators. I'm still at a loss here I appreciate the help but I just don't know how to correct the order of operations still.

public class Example : MonoBehaviour {

// Use this for initialization
void Start ()
{
Maths();

}

// Update is called once per frame
void Update () {

}
public static float Maths()
{
System.Random rand = new System.Random();
List<char> operators = new List<char>() { '+', '-', '/', '*' };
List<int> operatorsThis = new List<int>();
List<float> operands = new List<float>();
int operatorsCount = 2;
int operandsMin = 1;
int operandsMax = 10;
StringBuilder sb = new StringBuilder();
float f;
int r = -1;

for (int i = 0; i < operatorsCount; i++)
{
f = rand.Next(operandsMin, operandsMax + 1);
sb.Append(f.ToString() + " ");

r = rand.Next(4);
char c = operators[r];
sb.Append(c + " ");

}
f = rand.Next(operandsMin, operandsMax + 1);
sb.Append(f.ToString() + " ");
Debug.Log(sb.ToString());
f = operands[0];
int iLast = 0;

for (int i = 0; i < operatorsThis.Count; i++)
{
if (operatorsThis[i] >= 2)
{
iLast = i;
f = operands[i + 1];

if (operatorsThis[i] == 2)
{

}
if (operatorsThis[i] == 3)
{

}
}

if (operatorsThis[i] < 2)
{
iLast = i;
f = operands[i + 1];

if (operatorsThis[i] == 0)
{
}
else if (operatorsThis[i] == 1)
{
}
}
else
{
if (operatorsThis[i] == 2)
{
f /= operands[i + 1];
}
else if (operatorsThis[i] == 3)
{
f *= operands[i + 1];
}
if (i == operatorsThis.Count - 1)
{
if (operatorsThis[iLast] == 0)
{
}
else if (operatorsThis[iLast] == 1)
{