The problem with your current approach is that you're using randomization to get all the combinations of the variables you have, but that is not assuring you'll get them all.
The other problem seems to be in the logic of your code snippet: if at any point expression1 != expression2
you'll be out of the while-loop and the if-statement will be true, it will print the message dialog and then the program will exit. Since you're using randoms you'll get either the same values each time o different ones, but you will always get ONE possibility.
One approach would be creating a truth table which has all the combinations of truth of the variables involved and test your expression each time. For example:
private static void compareExpressions() {
final int NUMBER_OF_VARIABLES = 3;
final int NUMBER_OF_ROWS = (int) Math.pow(2, NUMBER_OF_VARIABLES);
boolean values[] = new boolean[NUMBER_OF_VARIABLES];
boolean expression1, expression2;
// Build a truth table
for(int i = 0; i < NUMBER_OF_ROWS; i++) {
for(int j = NUMBER_OF_VARIABLES - 1, k = 0; j >= 0; j--, k++) {
values[k] = (i / (int) Math.pow(2, j)) % 2 == 0 ? false : true;
}
// Compare what you need and show results for each row in the truth table
expression1 = !values[0] && (values[1] || !values[2]);
expression2 = !(values[0] || !values[1] && values[2]);
if(expression1 != expression2) {
System.out.println("b: " + values[0] + " c: " + values[1] + " d: " + values[2]);
}
}
}
You can create truth tables as big as you want with this approach but it will get tedious and more error prone as the number of variables increases because of the "unnamed" variables in the values[]
array or if you have different expressions.
Unfortunately solving that would require a parser for the expressions and a better way to store the variables (say, for example, a Map<String, Boolean>
).