×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Getting random answers for a quiz

Getting random answers for a quiz

Getting random answers for a quiz

(OP)
I am new to cgi, but with the help of several tutorials and forum searches I have been able to customize this script until this point. The quiz script already displays the questions randomly, but the answer choices are displayed in the order that I enter them. What do I need to do to tell it to make the answer choices display randomly? I have tried everything I can think of, but nothing has worked. Any help is GREATLY appreciated!  Here's the entire sub:

sub ShowQuiz {
    my $quiz = shift;
    my $num_ques = getNumLine("$quiz.db");

    $limit = param('number') > 0 ? param('number') : $num_ques;
    $limit = $num_ques if $limit > $num_ques;

    for ($i=0;$i<$num_ques;$i++) {$array[$i] = $i;}
    for ($i=0;$i<$num_ques;$i++) {
        $x = $array[int(rand($num_ques))];
        $y = $array[int(rand($num_ques))];

        $temp      = $array[$x];
        $array[$x] = $array[$y];
        $array[$y] = $temp;
        }

    splice(@array, $limit);

    flock(2, DB);
    open(DB, "$db_dir/$quiz.db") or error("Cannot open $quiz.db: $!");
    @lines = <DB>;
    close(DB);
    flock(8, DB);

    print "<form action=\"$script\" method=\"POST\">\n";
    print "<table border=0 cellspacing=0 cellpadding=0 bgcolor=#FFFFFF width=95%><tr><td>\n";
    print "<table border=0 cellspacing=1 cellpadding=4 width=100%>\n";
    print "<tr><td bgcolor=#FFFFFF><font$font_para size=4><b><i>".getQuizName($quiz)."</i></b></font><br></td></tr>\n";

    $i = 1;
    foreach $linenum (@array) {
        my $j = 'color' . (($i % 2) + 1);
        ($qtype, $question, $qimage, $answer, $aimage, $explanation, $choice) = split(/\|\|/, $lines[$linenum]);

        print "<tr bgcolor=#DFE9F9><td width=100%><font$font_para size=2><b>$i. $question</b>";
        print "<br><img src=\"http://$qimage\">" if $qimage ne "";
        if ($qtype eq "mc") {
            chomp($choice);
            @choices = split(/\`\`/, $choice);
            foreach $choice (@choices) {
                print "<br><input type=\"radio\" name=\"$linenum\" value=\"$choice\">$choice";
                }
            }
        if ($qtype eq "sa") {
            print "<br>Answer: <input type=\"text\" name=\"$linenum\" size=30>";
            }
        if ($qtype eq "nu") {
            print "<br>Answer: <input type=\"text\" name=\"$linenum\" size=30> (Enter only numeric value)";
            }
        if ($qtype eq "tf") {
            print "<br><input type=\"radio\" name=\"$linenum\" value=\"t\">True <input type=\"radio\" name=\"$linenum\" value=\"f\">False";
            }

        print "</font></td></tr>\n";

        $i++;

        }

RE: Getting random answers for a quiz

yikes! less code please. More explanation of what parts of the code are doing what. Use the code tags too please.

[code]
your code here
[/code]

------------------------------------------
- Kevin, perl coder unexceptional! wiggle

RE: Getting random answers for a quiz

(OP)
Lol, sorry about that. I'll try again.

I think the part I need help with is here (or at least this is the part that I have been editing, doing trial and error on:

CODE

if ($qtype eq "mc") {
            chomp($choice);
            @choices = split(/\`\`/, $choice);
            foreach $choice (@choices) {
                print "<br><input type=\"radio\" name=\"$linenum\" value=\"$choice\">$choice";
                }

I believe these lines tell it if the question is multiple choice (mc) then post the available answer choices for that question.  How can I tell it to post them randomly (i.e. the answers in a different order each time the page is reloaded).

Thank you, thank you for at least responding!!

RE: Getting random answers for a quiz

CODE

use List::Util qw/shuffle/;
if ($qtype eq "mc") {
   chomp($choice);
   my @choices = split(/\`\`/, $choice);
   @choices = shuffle(@choices);
   foreach $choice (@choices) {
      print "<br><input type=\"radio\" name=\"$linenum\" value=\"$choice\">$choice";
   }
}

Core (perl 5.8.8) Modules used :
  • List::Util - A selection of general-utility list subroutines

------------------------------------------
- Kevin, perl coder unexceptional! wiggle

RE: Getting random answers for a quiz

if that works, update any other forums you have this question posted on to let them know you have it working. Feel free to post the code to show how you got it working in case anyone has comments or suggestions.

------------------------------------------
- Kevin, perl coder unexceptional! wiggle

RE: Getting random answers for a quiz

(OP)
Ahhh... thank you!  You are a god :)  I had seen the utilities being used in other examples, but for some reason I thought you needed to insert the whole utility into the script as well... shows my knowledge, huh.  Thank you again.  And yes, I had already planned on posting the results in the other forum.  Off to do that right now.  Thanks again :) ~ Audrey

RE: Getting random answers for a quiz

you're welcome smile

------------------------------------------
- Kevin, perl coder unexceptional! wiggle

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close