2

I'm currently working on a school project in Android Studio. I want to write an algorithm which generates randomly equations, not just random numbers but actual equations like 30+48-18. I was thinking of the following steps:

  1. Generate a random number which indicates the amount of operations with an interval from 1 to 3. (I've allready written that code:

    Random rand1 = new Random();
    int operation = rand1.nextInt(2)+1;
    String StringOperation = String.valueOf(operation);
    
  2. Then for every operation randomly choose which operation (+,-,*,/). I'm struggeling to write the code for this step...

  3. The final step would be to randomly generate the acutal numbers which will appear in the actual equation. This would mean that you need (amount of operations)+1 numbers for the equation. This wouldn't be so hard to write the code for.

It would help me a lot if someone could help me with step 2 as I'm struggling to write the code for this step. I've already tried a few things but they all didn't work.

If anything is unclear, please write it so I can clarify the problems :)

Thank you already in advance!

ztv
  • 134
  • 1
  • 9
  • The Android tag is not very relevant. The programming laguage matters more. –  Oct 10 '16 at 14:43

2 Answers2

0

For step 2 you can store the operations in an array. Then you can use the function below to get the random operation

public static int getRandom(char[] array) {
int rnd = new Random().nextInt(array.length);
return array[rnd];
}

Alternatively, you can use a switch statement also after generating a random number between 1 and 4.

Netham
  • 1,168
  • 6
  • 16
0

Then for every operation randomly choose which operation (+,-,*,/). I'm struggling to write the code for this step...

In your finite operation set {+,-,*,/}, how would you randomly select the operation as a human? You would pick randomly by looking at their positions right? So the hint is to randomly choose the position in the range(0,3) and use the random position as an index to get the operation.

Try this without reading further.

String[] operationSet = new String[]{"+", "-", "/", "*"};

public void testGenerateRandomEquations() {

    Random random = new Random();
    int numOfOperations = random.nextInt(2) + 1;

    List<String> operations = new ArrayList<>();

    for (int i = 0; i < numOfOperations; i++) {
        String operation = operationSet[random.nextInt(3)];
        operations.add(operation);
    }

    int numOfNumbers = numOfOperations + 1;
    List<Integer> numbers = new ArrayList<>();

    for (int i = 0; i < numOfNumbers; i++) {
        int number = random.nextInt(Integer.MAX_VALUE) - random.nextInt(Integer.MAX_VALUE);
        numbers.add(number);
    }
    //Now you've the list of random numbers and operations. You can further randomize
    //by randomly choosing the number and operation from those list.
}
Gurupad Mamadapur
  • 989
  • 1
  • 13
  • 24
  • Thank you! I will try this and tell you if it worked for me :D – ztv Oct 10 '16 at 16:07
  • Happy to help. It'll work and also if you later want to compute the expression it's better to store the expression in a different format - infix or postfix. Read about them here - http://stackoverflow.com/questions/7562477/why-do-we-need-prefix-postfix-notation . – Gurupad Mamadapur Oct 10 '16 at 16:32
  • And now the actual equation is already completed? Or do I have to do anything else to complete the equation? – ztv Oct 10 '16 at 16:36
  • I hope you know about Lists. Suppose your expression is `a+b*c/d`, the `numbers` list contains `[a,b,c,d]` and `operations` list contains `[+,*,/]`. Now if you want to create an expression you can just alternatively fetch number from `[a,b,c,d]` and `[+,*,/]`. For example, to fetch number `a` you can call `numbers.get(0)` where`0` is the position of number `a` in the list. – Gurupad Mamadapur Oct 10 '16 at 16:41
  • But is there a simple command to just show up the random equation which was created by the algorithm above? – ztv Oct 10 '16 at 17:15