For example, if you had:

$a = non_zero_random(-9,9,1);

$b = non_zero_random(-9,9,1);

$d = non_zero_random(-9,9,1);

$e = non_zero_random(-9,9,1);

$ans3 = ($c/$a)-(($f*$a*$b-$c*$b*$d)/($e*$a*$a-$b*$d*$a));

$ans4 = ($f*$a-$c*$d)/($e*$a-$b*$d);

You could change it to

do {

$a = non_zero_random(-9,9,1);

$b = non_zero_random(-9,9,1);

$d = non_zero_random(-9,9,1);

$e = non_zero_random(-9,9,1);

} until ($e*$a - $b*$d != 0);

$ans3 = ($c/$a)-(($f*$a*$b-$c*$b*$d)/($e*$a*$a-$b*$d*$a));

$ans4 = ($f*$a-$c*$d)/($e*$a-$b*$d);

That will re-loop the variable initializations until neither denominator is 0.

Or an alternative is to define $a in a way that guarantees these denominators to be nonzero. It seems that if $a is nonzero, not ±1, and also relatively prime to $b*$d, that would prevent the denominators from being 0. So the line that makes $a could be:

do {$a = list_random(-9,..-2,2..9)} until (gcd($a,$b*$d) == 1);

With more information about how/why you are making $a, $b, $d, and $e, you could possibly define them all deterministically to avoid division by 0. (As opposed to these approaches above which are probabilistic and introduce some inefficiency because of running a loop.)