php - php - Random multiplication table generator - answerstu - answerstu.com answerstu

php - Random multiplication table generator

im creating an multiplication table system in PHP.

The input tho this system is an table number and the amount of questions. The table 1 is skipped so 2 to input table

At this point I just fill an array like this:

$nCount = $_POST['count'];
$nHighest = $_POST['table'];
$aSums = [];
$nCounter = 0;
while($nCount > 0){
    $cSumString = rand(2, $nHighest) . "*" . rand(1, 10);
    $aSums[$nCounter] = $cSumString
    $nCount--;
    $nCounter++;
}

I want to parse the questions in multiple ways:

3 * 5 = ... (normal)
... * 5 = 25 (first number to fill in)
8 * ... = 16 (second to fill in)

This needs to be randomised.

Example:

1 * 2 = ... 
6 * ... = 12 
... * 4 = 20
8 * 4 = ...
2 * ... = 6

The only thing I know that should be able to so this is a switch but I cant seem to get it to work propperly so if anyone can give me a push in the right direction i would appreciate it. Im not asking for instacode just some tips would be great.

3 Answers

  1. Loren- Reply

    2019-11-13

    $nCount = $_POST['count'];
    $nHighest = $_POST['table'];
    $aSums = [];
    $leftOut = ['leftFactor', 'rightFactor', 'product'];
    
    for ($i = 0; $i <= $nCount; $i++) {
      $leftOutRand = rand(0, count($leftOut) - 1);
      $factor = rand(1, $nHighest);
      $product = rand(0, $nHighest) * $factor;
      switch($leftOut[$leftOutRand]) {
        case 'leftFactor':
          $cSumString = '...' . ' * ' . $factor . ' = ' . $product;
          break;
        case 'rightFactor':
          $cSumString = $factor . ' * ' . '...' . ' = ' . $product;
          break;
        case 'product':
          $cSumString = rand(1, $nHighest) . ' * ' . rand(1, $nHighest) . ' = ' . '...';
          break;
      }
      $aSums[$i] = $cSumString;
    }
    

    example Output:

    2 * ... = 4
    ... * 2 = 6
    1 * 1 = ...
    ... * 2 = 6
    3 * ... = 12
    2 * ... = 2
    2 * 4 = ...
    4 * ... = 0
    3 * 1 = ...
    2 * 1 = ...
    ... * 4 = 16
    1 * ... = 2
    ... * 2 = 2
    1 * 4 = ...
    3 * ... = 9
    2 * 3 = ...
    2 * ... = 6
    ... * 1 = 3
    ... * 4 = 4
    1 * 1 = ...
    ... * 3 = 9
    
  2. Lori- Reply

    2019-11-13

    <?php
    
    $total = 4;
    $table = 7;
    
    $generate_pair = function() use ($table)
    {
        $first = rand(1, 10);
        $last  = rand(2, $table);
    
        return [$first, $last];
    };
    
    $pairs = [];
    while (count($pairs) < $total) {
        $pair = $generate_pair();
        if(!in_array($pair, $pairs))
            $pairs[] = $pair;
    }
    
    $questions = array_map(function ($v) {
        return sprintf('%d * %d', $v[0], $v[1]);
    }, $pairs);
    
    var_dump($questions);
    

    Example output:

    array(4) {
      [0]=>
      string(5) "7 * 3"
      [1]=>
      string(5) "5 * 7"
      [2]=>
      string(5) "5 * 2"
      [3]=>
      string(5) "6 * 2"
    }
    
  3. Lorin- Reply

    2019-11-13

    I would personally put it in an array and read it from there then its easier to get it trough an switch like Mark labenski mentioned and I would do it like this:

    $questpertable = floor($questions / ($table-1));
    $nTableCounter = 2; 
    $array = [];
    for ($nX = 0; $nX < ($table-1); $nX++){
      for ($nY = 0; $nY < $questpertable;){
          $left = rand(1, 10);
          $right  = $tablecounter;
          $answer = $left * $right;     
          $array[$nY] = array($left, $right, $answer );
          $Y++;                   
      }
      $tablecounter++;
    }
    

    The only thing that misses here is the residual value u will have to find something to fix this.

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>