简体   繁体   中英

How to generate random operator after each random operand using php?

Here is my code for random operand generator:

$randomOperands = array();

// do a loop over $i n times
for ($i = 0; $i <= 10; $i ++) {
    // assign random operand to array slot
    $randomOperands[] = rand(0, 9);
}
// print array values:
foreach($randomOperands as $key=>$value1){
    echo  $value1 "<br />"; 
}

But how can I generate random operator just after each random operand?

This is my form through which user will give his input and the n no.of questions having random sequence containing n(no.of operands user wants, he can give input for this within the range of 1 to 10 ) operands and n-1 operators and answer of the sequence will be generated after clicking on generate button.

   <form action="" method="POST">
   Select no.of questions:<input type="number" name="que" value="que">
   <br>
   <br>
   Select no. of series: <select name="select">
    <option value="0"> 2 </option>
    <option value="1"> 3 </option>
    <option value="2"> 4 </option>
    <option value="3"> 5 </option>
    <option value="4"> 6 </option>
    <option value="5"> 7 </option>
    <option value="6"> 8 </option>
    <option value="7"> 9 </option>
    <option value="8"> 10 </option>
   </select>

     <br><br>
     Select number type(in digits) :
     <select name="select_box">
    <option value="0">1</option>
    <option value="1"> 2 </option>
    <option value="2"> 3 </option>
    <br><br>
  </select>
  <br /><br />
   Select operations:<br />
   <input type="checkbox" id="add" name="operation" value="addition"  
   onclick="doOperation()"><label>Addition</label>
   <input type="checkbox" id="sub" name="operation" value="subtract"  
    onclick="doOperation()"><label>Subtraction</label>
   <input type="checkbox" id="mult" name="operation" value="multiply"  
   onclick="doOperation()"><label>Multiplication</label>
   <input type="checkbox" id="div" name="operation" value="divide"  
    onclick="doOperation()"><label>Division</label>
    <br><br>

     <br><br>
     <input type="submit" name="submit" value="Generate"><br>
    <br>
   </form>

here in the form 1st field is no.of questions ie how many questions user wants to get having random sequence. 2nd field is no. of series ie how many operands user wants in that sequence (if he gave input for 6 or any number between 1 to 10 then the sequence of 6 operands and 5 operators and their ans. will be generated at the end after filling all fields). 3rd field is no. of digits ie for how many digits user wnat to generate a sequence, I have given choces only upto 3 digits. at last, which operations user want to perform for sequence

I know its very tricky task but interseting too. how can I do it?

Here's how I would generate a string of integers and operands.

Note how extra checks are needed to avoid division by zero.

Also, as asked above, do you actually want to evaluate the results in code?

 <?php

$potentialOperands = array('+', '-', '*', '/');  //Keep division as the last operand here
$previousInt = null;        //Track the previous operand, else we'll get division by zero

$randomIntegers = array();
$randomOperands = array();

 // do a loop over $i n times
 for ($i = 0; $i <= 10; $i ++) {
    // assign random operand to array slot
    $nextInt = rand(0, 9);
    $randomIntegers[] = $nextInt;
    if($i < 10){    //No operand after last integer
        if($previousInt === 0){
            //Make sure to avoid a potenial division-by-zero
            $randomOperands[] = $potentialOperands[rand(0, 2)];
        }
        else{
            $randomOperands[] = $potentialOperands[rand(0, 3)];
        }
    }
    $previousInt = $nextInt;
 }

 // print array values:
   $exp = '';
   foreach($randomIntegers as $key=>$value1)
   {
       $exp .=  $value1 . " "; 
       if(isset($randomOperands[$key])){
           $exp .=  $randomOperands[$key] ." "; 
       }
   }

   $res = eval("return ($exp);");

   //Generates answer rounded to 3 decimal places
   //The answer uses standard PHP operator precedence
   printf("%s = %.3f", $exp, $res);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM