# Evaluating a list of inequalities with random numbers [closed]

I have a list of inequalities, which include in total four parameters, that I would like to test by feeding the four parameters with random numbers. The goal is to find the best values of the variables such that they satisfy all inequalities.

The first problem I encounter is that I do not know how to "feed" a variable with random numbers. For instance, assume that $0<a\leq5$, $1<b<2$ and $a+b<8$. Now I want Mathematica to find random values for $a$ and $b$ such that both inequalities are fulfilled. If I let it run often enough it will eventually tell me that $a=5$ and $b=2$, which would be the the optimal result for me.

• Have you tried FindInstance? It implements many methods for finding values that satisfy the inequalities, possibly including random search. – Szabolcs Oct 15 '15 at 14:35
• BTW $a=5$ does not satisfy $0<a<5$. – Szabolcs Oct 15 '15 at 14:37
• Thanks for the quick response. I tried 'FindInstance', however, for this simple example it always gives back the same result, no matter how often I evaluate it. – costalmu Oct 15 '15 at 14:40
• From what I understood from the question, you were looking for one set of numbers that satisfies the inequalities. If this is not the case, please clarify. But you can use the additional arguments of FindInstance to get more possible solutions. – Szabolcs Oct 15 '15 at 14:56

Assuming that a and b are integers, then 1 < b < 2 cannot be satisfied; at least one inequality must be <=. Use Reduce or Solve.

Reduce[{0 < a <= 5, 1 < b < 2, a + b < 8}, {a, b}, Integers]

(*  False  *)

Reduce[{0 < a <= 5, 1 < b <= 2, a + b < 8}, {a, b}, Integers]

(*  (a == 1 && b == 2) || (a == 2 && b == 2) || (a == 3 && b == 2) || (a == 4 &&
b == 2) || (a == 5 && b == 2)  *)

Solve[{0 < a <= 5, 1 < b <= 2, a + b < 8}, {a, b}, Integers]

(*  {{a -> 1, b -> 2}, {a -> 2, b -> 2}, {a -> 3, b -> 2}, {a -> 4,
b -> 2}, {a -> 5, b -> 2}}  *)

Reduce[{0 < a <= 5, 1 <= b < 2, a + b < 8}, {a, b}, Integers]

(*  (a == 1 && b == 1) || (a == 2 && b == 1) || (a == 3 && b == 1) || (a == 4 &&
b == 1) || (a == 5 && b == 1)  *)

Solve[{0 < a <= 5, 1 <= b < 2, a + b < 8}, {a, b}, Integers]

(*  {{a -> 1, b -> 1}, {a -> 2, b -> 1}, {a -> 3, b -> 1}, {a -> 4,
b -> 1}, {a -> 5, b -> 1}}  *)

Reduce[{0 < a <= 5, 1 <= b <= 2, a + b < 8}, {a, b}, Integers]

(*  (a == 1 && b == 1) || (a == 1 && b == 2) || (a == 2 && b == 1) || (a == 2 &&
b == 2) || (a == 3 && b == 1) || (a == 3 && b == 2) || (a == 4 &&
b == 1) || (a == 4 && b == 2) || (a == 5 && b == 1) || (a == 5 && b == 2)  *)

Solve[{0 < a <= 5, 1 <= b <= 2, a + b < 8}, {a, b}, Integers]

(*  {{a -> 1, b -> 1}, {a -> 1, b -> 2}, {a -> 2, b -> 1}, {a -> 2,
b -> 2}, {a -> 3, b -> 1}, {a -> 3, b -> 2}, {a -> 4, b -> 1}, {a -> 4,
b -> 2}, {a -> 5, b -> 1}, {a -> 5, b -> 2}}  *)


Also look at ToRules

And the version with FindInstance:

c = 2;
FindInstance[0 < a <= 5 && 1 < b < 2 && a + b < 8, {a, b}, c]
{{a -> 9/41, b -> 133/102}, {a -> 5, b -> 7/6}}


You can increase the variable c to get more solutions. if a and b are Integers:

FindInstance[0 < a <= 5 && 1 < b <= 2 && a + b < 8, {a, b}, Integers,c]

{{a -> 2, b -> 2}, {a -> 4, b -> 2}}


Note that in this case 1 < b <= 2