WeBWorK Problems

Where are things like random(a,b,c) defined?

Where are things like random(a,b,c) defined?

by Ted Cox -
Number of replies: 6
As far as I can tell, "random," as in random(a,b,c), is not perl. I have spent quite a bit of time trying to find its definition. Can someone please point me to the right place?

Thanks in advance.
In reply to Ted Cox

Re: Where are things like random(a,b,c) defined?

by Michael Gage -
Hi Ted,

This particular macro is defined in the file PGbasicmacros.pl

Here are the pod docs: http://webwork.maa.org/doc/cvs/pg_CURRENT/

http://webwork.maa.org/doc/cvs/pg_CURRENT/macros/PGbasicmacros.pl.html

This database lists the file in which each macro is defined:

http://wwrk.maa.org/moodle/mod/data/view.php?id=5

and if you can look at the source code here if you haven't downloaded your own webwork site:

http://cvs.webwork.rochester.edu/viewcvs.cgi/pg/

Most importantly the basic templates and the Problem Techniques links on the wiki's Author section give a lot of good examples about how to get started writing questions using the current best practices:
http://webwork.maa.org/wiki/Category:Authors

The hard part of getting started is finding a reasonable collection of documentation, so I hope this helps.

-- Mike

In reply to Michael Gage

Re: Where are things like random(a,b,c) defined?

by Ted Cox -
Hi Mike, Thanks for the reply and information. It has been rather frustrating trying to find out exactly what random(a,b,c) really returns (to be clear about boundary cases, for instance). I looked at much of http://webwork.maa.org/wiki/Category:Authors without success. I also missed any reference in general documentation that one should go to http://wwrk.maa.org/moodle/mod/data/view.php?id=5 to find definitions, and did not suspect random was to be found inside of http://webwork.maa.org/doc/cvs/pg_CURRENT/macros/PGbasicmacros.pl.html. My issue is not writing problems, it's knowing what the WW functions/macros do so that I know my problems are doing what I want them to do. Anyway, I know where to go now, so thanks again. WW is a great system.

Ted
In reply to Ted Cox

Re: Where are things like random(a,b,c) defined?

by Michael Gage -
Ted,

I'm glad it was helpful. I'm well aware that it can be hard to find information when you are getting started.

Let me take this opportunity to invite everyone to contribute to the documentation on the wiki -- additions -- such as a suggestion to look at the database are welcome. (The database by the way is also not complete anymore -- it was compiled a few years ago -- but it can still be useful.) And if someone would like to add a larger section (such as the Problem Techniques section that Gavin LaRose pioneered) that would be welcome as well.

If someone wants to take charge of reorganizing existing sections I'd be delighted to discuss it with you.

-- Mike

In reply to Michael Gage

Re: Where are things like random(a,b,c) defined?

by Jason Aubrey -
I was trying to chase through the definition of random(a,b,c) in the code, and it's basically clear to me how it works. However, I'm a little unclear about how the problem seed is first initialized. If I edit a problem in the library, the seed has clearly not been defined, and (I think) the line

$self->{problemSeed} = '123456' unless ...

in PGProblemEditor.pm initializes the seed (but why 123456 and not 1234 as it appears in the problem editor?).

On the other hand, if I add a new blank problem from the homework set editor, it comes with a 'random' seed.

This is the part I'm not clear about: My interpretation of this is that a new PGrandom object is created for each individual problem exactly when it is first added to a homework set. Something like blankProblem.pg has been used over and over again, so its seed has been recalculated repeatedly by the PGrandom code. When a set is assigned to the students, PGrandom calculates the seed for each student by looking at the seed for that problem given to the previous student. If this is right, where would the (unassigned) seed for blankProblem.pg be stored? And, under this interpretation, in a brand new WW course, the first time I add blankProblem.pg to an assignment, it's seed should be 1234.

Is this on something like the right track?

Thanks,
Jason
In reply to Jason Aubrey

Re: Where are things like random(a,b,c) defined?

by Michael Gage -
Only a problem seed (a number) is created for each student when they are assigned that question in a homework set. The PGrandom object is created (from that problem seed) when the question is being rendered for the student to view. The principal chain of command is in Instructor.pm where
assignSetToUser()
....
foreach my $GlobalProblem (@GlobalProblems) {
		my @result = $self->assignProblemToUser($userID, $GlobalProblem);
		push @results, @result if @result and not $set_assigned;
	}
is defined. It in turn calls
sub assignProblemToUser {
	my ($self, $userID, $GlobalProblem, $seed) = @_;
	my $db = $self->{db};
	
	my $UserProblem = $db->newUserProblem;
	$UserProblem->user_id($userID);
	$UserProblem->set_id($GlobalProblem->set_id);
	$UserProblem->problem_id($GlobalProblem->problem_id);
	initializeUserProblem($UserProblem, $seed);
....
notice that assignProblemToUser is called in such a way as to leave $seed undefined. In turn initializeUserProblem which resides in DB::Utils.pm is called. This is where the random seed for the student is chosen if it has not already been defined and where it is stored in the data the problem_user table of the database.
# Populate a user record with sane defaults and a random seed
# This function edits the record in place, so you can discard
# the return value.
sub initializeUserProblem {
	my ($userProblem, $seed) = @_;
	$seed = int rand 5000 unless defined $seed;
	$userProblem->status(0.0);
	$userProblem->attempted(0);
	$userProblem->num_correct(0);
	$userProblem->num_incorrect(0);
	$userProblem->problem_seed($seed);
	return $userProblem;
}


The PGrandom object is created and initialized at around line 239 of PG.pl. The seed can be changed using SRAND defined in PGbasicmacros.pl
sub SRAND { # resets the main random generator -- use cautiously
    my $seed = shift;
	$PG_random_generator -> srand($seed);
}

Hope this helps.
In reply to Jason Aubrey

Re: Where are things like random(a,b,c) defined?

by Michael Gage -
in PGProblemEditor.pm initializes the seed (but why 123456 and not 1234 as it appears in the problem editor?). This is because of inconsistency between the default in the PGProblemEditor.pm at line 1181
sub view_handler {
	my ($self, $genericParams, $actionParams, $tableParams) = @_;
	my $courseName      =  $self->{courseID};
	my $setName         =  $self->{setID};
	my $fullSetName     =  $self->{fullSetID};
	my $problemNumber   =  $self->{problemID};
	my $problemSeed     = ($actionParams->{'action.view.seed'}) ? 
	                                $actionParams->{'action.view.seed'}->[0] 
	                                : 1234;
and the defaults at lines 194 and 334. For consistency I'll change the default at line 1181. Problems in the Library browser have temporarily been assigned to the "Undefined" set in a somewhat ad hoc way and I'm not surprised that there are inconsistencies between the behavior in editing an unassigned library problem and one assigned to a set. There is a good deal of code that is repeated in these operations and refactoring the code and making the behavior consistent would be a good "summer of code" project.