## Forum archive 2000-2006

### Dan Maynard - Random Number Generator

by Arnold Pizer -
Number of replies: 0
 Random Number Generator topic started 7/15/2002; 7:46:43 AMlast post 7/24/2002; 4:33:32 AM
 Dan Maynard - Random Number Generator  7/15/2002; 7:46:43 AM (reads: 2456, responses: 6) I am trying to write a set of problems to be used as review. What I am hoping to implement is a system where for a given problem, every time a student logs into the system, they have a new set of random number in their problems. Since they have infinite tries, and it is not graded, this will give theminfintely many practice problems--a new set each time they log in. The current random number generator stores the random numbers for each student so that when they login and log out, the problem remains the same. There will be over 600 students in this class, so actually creating a large amount of separate problems for each student seems relatively out fo the question. If anyone is aware of a way to make the problems different on each login, or if you have any other advice on this topic, please let me know. Thanks. Dan Maynard, University of New Hampshire <| Post or View Comments |>

 Gavin LaRose - Re: Random Number Generator  7/16/2002; 7:47:18 AM (reads: 2756, responses: 0) Hi Dan, c.f. the PGbasicmacros documentation: PGbasicmacros.pl. In the section on the random number generator there, it includes the comments (slightly edited)  SRAND(seed) # resets the main random generator -- use very # cautiously SRAND(time) # will create a different problem every time it is # called. This makes it difficult to check answers :-). SRAND($envir{'inputs_ref'}->{'key'} ) # will create a different problem for each login session. This is # probably what is desired It sounds to me as if the last is what you're looking for. I'd put the SRAND call at the beginning of the problem file, and the rest should take care of itself. Gavin <| Post or View Comments |>  Dan Maynard - Re: Random Number Generator 7/16/2002; 9:48:46 AM (reads: 2766, responses: 0) Gavin,I tried using the third option...should I be altering the line$envir{'inputs_ref'}->{'key'} at all, such as actually putting in a value for key? When I place this line at the top of the specific problem (also, does it matter where in the problem it is placed?), I get the error: Argument "secwNqXqoK5*wrOMatto116*A8^EBzNp49elR0FN" isn't numeric in multiplication (*) at /web/webwork/system/courseScripts/PGrandom.pm Any ideas? Dan <| Post or View Comments |>

 Gavin LaRose - Re: Random Number Generator  7/17/2002; 12:38:52 AM (reads: 2776, responses: 0) Hi Dan, Oops. You're right, that would be a problem. The idea is to use some randomly generated number to reset the seed for the problem every time the student logs in. A reasonable choice is the key that is generated for the student which tells the system that he or she has logged on and can continue working with the system. However, the documentation for PGrandom assumes that the key is numeric, which as you found out isn't the case. I was dealing with this a bit ago and ended up with the work-around of just changing all non-numeric characters in the key into numbers (I didn't look for this before posting before; sorry about that oversight). Something like the following should work.  $sessionKey =$envir{'inputs_ref'}->{'key'}; # grab the key $sessionKey =~ s/[^~~w~~d]//g; # delete non alphanumerics$sessionKey =~ tr/[A-Z]/[a-z]/; # lowercase all letters $sessionKey =~ tr/[a-z]/[0-90-90-5]/; # turn a into 0, b into 1... SRAND($sessionKey);This gives a pretty big number for the seed, but I don't know if that really matters. You could truncate it by just taking a substring of it (e.g., $sessionKey = substr($sessionKey,0,5); to grab the first five digits). Does that help? Gavin <| Post or View Comments |>

 Dan Maynard - Re: Random Number Generator  7/18/2002; 1:56:32 AM (reads: 2732, responses: 0) Gavin,I'm still having some issues with it. I copied the above script, and placed this in the specific problem that I want to re-generate the random numbers. I tried having it called in different places within the document....no luck. Should I be placing it somewhere different, or am I calling it incorrectly. For instance, do I have to call it within the script after each time I use the random() function? Or do I actually use it to generate the random numbers? Dan <| Post or View Comments |>

 Gavin LaRose - Re: Random Number Generator  7/18/2002; 6:30:30 AM (reads: 2849, responses: 0) Hi Dan, Ok, so the length of the seed does seem to matter. The following problem file seems to work. Note that the discussion board software might hide or get rid of backslashes or other special characters, so proofread it for omissions of that sort. (Note that you only want one SRAND function call. If you call SRAND with the same seed at the beginning of every problem in a set you'll find the random numbers that are showing up in each problem are the same. This is because once the seed is fixed the pseudorandom numbers are algorithmically determined. In this case we're using the session key for the seed, and the session key is unique to the login session, not to every problem.) Gavin ##DESCRIPTION## Sample random seed problem####ENDDESCRIPTIONDOCUMENT();loadMacros(PG.pl,PGbasicmacros.pl,PGchoicemacros.pl,PGanswermacros.pl,PGauxiliaryFunctions.pl);TEXT(&beginproblem);$showPartialCorrectAnswers = 1;$sessionKey = $envir{'inputs_ref'}->{'key'}; # grab the key$sessionKey =~ s/[^~~w~~d]//g; # delete non alphanumerics$sessionKey =~ tr/[A-Z]/[a-z]/; # lowercase all letters$sessionKey =~ tr/[a-z]/[0-90-90-5]/; # turn a into 0, b into 1...$sessionKey = substr($sessionKey,0,4); # just take the first four # digitsSRAND($sessionKey);$a = random(1,10,1);BEGIN_TEXT$PAR$BBOLD Math Sample Homework $EBOLD$PARThe seed used this time is$BR$sessionKey$PARA random variable is$BR$a$PAREnter the number 1: \{ ans_rule(3) \}END_TEXTANS( num_cmp(1) );ENDDOCUMENT(); <| Post or View Comments |>

 Dan Maynard - Re: Random Number Generator  7/24/2002; 4:33:32 AM (reads: 2755, responses: 0) Thanks, that worked out great. Dan <| Post or View Comments |>