I have a site that requires random mathematical problems to be generated based on 3 parameters: operator (addition, subtraction, multiplication, division), regrouping (to carry or not to carry), and columns (one or two). I've been trying to wrap my head around how to go about doing this, but everything I come up with has a flaw in some way or other.
Here is what I am working with currently:
function create_problem($operator, $regrouping, $columns){
$top = rand(1,9);
$bottom = rand(1,9);
if($operator == "+"){
if($columns == 1 && $regrouping === false){
$result = array(
'top' => $top,
'bottom' => $bottom,
'formula' => "$top.$operator.$bottom"
);
}
if($columns == 2){
if($regrouping === false){
if($top+$bottom > 9){
$diff = ($top+$bottom)-10;
$top = $diff+rand(1, 3);
}
$result = array(
'top' => $top,
'bottom' => $bottom,
'formula' => "$top.$operator.$bottom"
);
}else{
if($top+$bottom < 10){
$top = rand(1, $bottom+1);
}
}
}
}
return $result;
}
If anyone has dealt with this, or if anyone has any pointers, I would be most appreciative!