Created
June 6, 2011 16:58
-
-
Save SeanJA/1010643 to your computer and use it in GitHub Desktop.
Generate a random math question that always has a positive answer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Starting with an answer, generate a math question to get the result | |
* @param int $answer Your desired answer | |
* @return string The equation to arrive at that answer | |
*/ | |
function generate_simple_math($answer){ | |
$operations = 2; | |
$min_number = 1; | |
$temp_answer = $answer; | |
$op_convert = array('+'=>'-', '-'=>'+'); | |
$ops = array('-', '+'); | |
$operators = array(); | |
for($i = 0; $i < $operations; $i++){ | |
$operators[] = $ops[rand(0,1)]; | |
} | |
$question = array(); | |
$numbers = array(); | |
for($i = 0; $i < $operations; $i++){ | |
$max_number = $temp_answer - 1; | |
$num = rand($min_number,$max_number); | |
$numbers[] = $num; | |
$temp_answer = eval('return ' . $temp_answer . ' ' .$operators[$i] . ' ' . $num. ';'); | |
//push the values onto the stack in the correct order | |
$question[] = $op_convert[$operators[$i]]; | |
$question[] = $num; | |
} | |
//add 1 to one of the other operands, set temp_answer to be -1 because 0s are hard apparently | |
if($temp_answer == 0){ | |
$question[1] = $question[1] + 1; | |
$temp_answer = -1; | |
} | |
//push the remainder on to the front | |
if($temp_answer > 0){ | |
array_unshift($question, $temp_answer); | |
//put the remainder on the end if it is less than 0 because starting with a negative is hard | |
} else { | |
//first operator is the one that is accociated with the | |
$op = array_shift($question); | |
$question[] = $op; | |
$question[] = $temp_answer; | |
} | |
$question = implode($question, ''); | |
//might have double operators because of "no zeros" requirement | |
$return_question = str_replace(array('--', '-+', '+-', '++'), array('+', '-', '-', '+'), $question); | |
return $return_question; | |
} | |
//no problems in 100000 iterations... probably safe... but not for sure | |
for($i = 0; $i < 100000; $i++){ | |
$min_answer = 1; | |
$max_answer = 15; | |
$answer = rand($min_answer,$max_answer); | |
$question = generate_simple_math($answer); | |
$result = eval('return '.$question.';'); | |
//major error here... this should be investigated | |
if($result !== $answer){ | |
$var_dump = array( | |
'question'=>$question, | |
'answer'=>$answer, | |
'eval_result'=>$result, | |
); | |
print_r($var_dump); | |
die(); | |
} | |
if(strpos($question , '0') !== false){ | |
$var_dump = array( | |
'question'=>$question, | |
'answer'=>$answer, | |
'eval_result'=>$result, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this is a bad idea... don't use this for anything... it just barely works