Skip to content
Advertisement

Randomly choose between of +, -, or * and apply it to two numbers

Good day

I am new to java and would like to know if there is a way for me to randomly choose between multiplication and addition and apply it to two numbers.

Any guidance would be appreciated.

Advertisement

Answer

I guess a code like the following could work:

// given two number a, b

double rand = Math.random();
if (rand > 0.5)
   c = a*b;
else
   c = a+b;

// c is the result of the addition/multiplication
1 People found this is helpful
Advertisement