phptest-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Phptest-users] To Randomize or not to Randomize


From: TW-2
Subject: Re: [Phptest-users] To Randomize or not to Randomize
Date: Sun, 19 Jan 2003 04:15:42 -0700

Sorry, I should post the whole function so you can see what's going on, the
previous post was missing a } bracket at the end of the function too. Bad
TW, Bad......

Here's the whole function, follow along if you can:

-------------------------

    function randomize($required)
    {
        global $db, $user, $questions;
        $result = $db->query("SELECT question_id FROM questions WHERE
required = $required AND subject_id = {$this->info['subject_id']}");
        // get number of questions in skill level
        if (!$numrows = $db->num_rows($result)) {
            return FALSE;
        }

        $count = 0;

// randomize questions 'off ' statement
  while ($row = $db->fetch_object($result)) {
                $this->questions[$count++] = $row->question_id;
            }

/******************/
/*
/* Turned off randomize for tests here, to enable undo the comment lines
/* Change line "while ($row......."  With one above to re-enable randomize
/*

  // put the question_ids of each question into an array
  // randomize questions 'on' statement
        while ($row = $db->fetch_object($result)) {
            $q[++$count] = $row->question_id;
        }

/*****************


  // initialize random number check array
        $check = array();

        $num_questions = $required ? $this->info['num_required'] :
$this->info['num_random'];

        // for each question
        for ($i = 1; $i <= $num_questions; $i++) {

            do {
                $flag = FALSE;

                // get a random number between 1 and $numrows
                if ($numrows > 1) {
                    $rand = mt_rand(1, $numrows);
              } else {
                    $rand = 1;
                }

                // iterate through our hash array to make sure the number
hasn't already been picked
                foreach($check as $value) {

                    // if it has...
                    if ($value == $rand) {
                        // go through the loop again and pick another number
                        $flag = TRUE;
                        break;
                    }

                }

            } while ($flag);

            // hash the picked number in the $check array so it can be
checked for future iterations
            $check[$i] = $rand;
            // store the question id in an array
            $this->questions[$this->count++] = $q[$rand];
        }
*/
    }

------------------------




reply via email to

[Prev in Thread] Current Thread [Next in Thread]