## WeBWorK Problems

### randomizing word pairs in question text.

by tim Payer -
Number of replies: 11
Hello All,

I would like to use some randomized word pairs in my question text.
These word pairs do not have to be tied to the numerical answers of the problem that I am using.

For example
"Given that 23% of a flock of birds has the bird flu what is the probability of drawing two birds from the flock that have bird flu"?

Where in the example above the word variable of flock would be replaced by:
flock = gaggle, murder, murmuring, etc...
Paired with these flock names would be the associated bird name
birds = geese, crows, starlings, etc...

Much appreciated!

Tim

by John Jones -

$a = list_random(['gaggle', 'geese'],['murder','crows'],['murmuring', 'starlings']); Then if$a->[0] is murder, then $a->[1] is crows. John In reply to John Jones ### Re: randomizing word pairs in question text. by Paul Pearson - I like John's solution, but here's one that uses more basic data structures. @birds = ('geese','crows','starlings'); # create parallel arrays @flock = ('gaggle','murder','murmuring');$i = random(0, 2, 1); # choose an array index at random
# $i = random(0,$#birds, 1); # choose an array index at random, more fancy

A $flock[$i] of $birds[$i] ...

This approach could also be generalized using a hash (associative array).

Paul Pearson

### Re: randomizing word pairs in question text.

by tim Payer -
Thanks Paul!

I will give that a try!

What would happen if a had triples instead or pairs to account for the singular and plural spellings?

Could this also be used for that approach?

Tim

### Re: randomizing word pairs in question text.

by Paul Pearson -
# create three arrays of the same length "in parallel"
@bird = ('goose','crow','starling');
@birds = ('geese','crows','starlings');
@flock = ('gaggle','murder','murmuring');
$i = random(0, 2, 1); # choose an array index at random BEGIN_TEXT A$bird[$i] flew with a$flock[$i] of$birds[$i] ... END_TEXT BEGIN_PGML A [$bird[$i] ] flew with a [$flock[$i] ] of [$birds[$i] ] ... END_PGML In reply to Paul Pearson ### Re: randomizing word pairs in question text. by tim Payer - Hi Paul, I liked your solution to my word triple problem, but unfortunately the arrays are non-responsive as I have entered them. Can you see any problems? I think I entered your arrays as given... Thanks, for any help. Tim # DESCRIPTION Function translations # Translate descriptions of critical points and function # into correct notation. # WeBWorK problem written by TimPayer <tsp1@humboldt.edu> # ENDDESCRIPTION ## DBsubject(Probability) ## DBchapter(Random variables) ## DBsection(Expectation) ## Institution(Humboldt State University) ## Author(Tim Payer) ## KEYWORDS(probability, translate, notation) DOCUMENT(); loadMacros( "PGstandard.pl", "PGunion.pl", "MathObjects.pl", "parserPopUp.pl", "PGML.pl", "PGcourse.pl", ); #Text(beginproblem()); #uncomment install_problem_grader(~~&std_problem_grader);$showPartialCorrectAnswers = 1;

Context("Numeric");

# create three arrays of the same length "in parallel"
@bird = ('goose','crow','starling');
@birds = ('geese','crows','starlings');
@flock = ('gaggle','murder','murmuring');
$i = random(0, 2, 1); # choose an array index at random$popup1 = PopUp(
["probability notation", "P(V)", "P(N)", "P(VV)", "P(NN)", "P(VN)", "P(NV)",
"P(VN  U  NN)", "P(NV  U  NN)", "P(VN   $\cap$   NN)", "P(NV   $\cap$   NN)",
"P(VN   $\cap$   NV  U  NN)", "P(VN  U  NV  $\cap$  NN)", "P(VN  U  NV  U  VV)",
"P(VN   $\cap$   NV  $\cap$  NN)", "P(NN   $\cap$   NV  U  VN)", "P(NN  U  NV  $\cap$  VV)",
"P(NN   $\cap$   NV   $\cap$  VV)","P(VN   $\cap$  NV) + P(NN)", "P(VN  $\times$  NV) + P(NN)",
"P(VN   $\cap$   NN) + P(NV   $\cap$   NN)",], "P(VV)");

$popup2 = PopUp( ["probability notation", "P(V)", "P(N)", "P(VV)", "P(NN)", "P(VN)", "P(NV)", "P(VN U NN)", "P(NV U NN)", "P(VN $\cap$ NN)", "P(NV $\cap$ NN)", "P(VN $\cap$ NV U NN)", "P(VN U NV $\cap$ NN)", "P(VN U NV U VV)", "P(VN $\cap$ NV $\cap$ NN)", "P(NN $\cap$ NV U VN)", "P(NN U NV $\cap$ VV)", "P(NN $\cap$ NV $\cap$ VV)","P(VN $\cap$ NV) + P(NN)", "P(VN $\times$ NV) + P(NN)", "P(VN $\cap$ NN) + P(NV $\cap$ NN)",], "P(VN U NV U VV)"); BEGIN_PGML 1. A flock of [$birds[$i] ] is described as a [@flock[$i] ] of [$birds[$i] ].
Consider the case where (percent)% of a [@flock[$i] ] of [$birds[$i] ] carry a bird flu virus, and that two [$birds[$i]] are drawn at random from the [@flock[$i] ], find the following probabilities given the following variable declarations:

Let V = Event that a [$bird[$i] ] has the bird flu virus.

Let N = Event that a [$bird[$i] ] does not have the bird flu virus.

1a.) What is the probability of randomly drawing two [$birds[$i] ] that both have the bird flu virus? Choose the correct probability notation below and then calculate its associated probability with fourth decimal accuracy.

[___]{$popup1} = 1b.) What is the probability of randomly drawing two [$birds[$i] ] such that at least one [$bird[$i]] has the bird flu virus? Choose the correct probability notation below and then calculate its associated probability with fourth decimal accuracy. [___]{$popup2} =

END_PGML

BEGIN_PGML_SOLUTION

END_PGML_SOLUTION

ENDDOCUMENT();

### Re: randomizing word pairs in question text.

by Paul Pearson -

https://raw.githubusercontent.com/openwebwork/pg/master/macros/PGML.pl

since PGML.pl versions before June 2015 did not handle nested square brackets properly.  If you don't have command line access on the server to install PGML.pl server wide, install it for a particular course by clicking "File Manager" and saving PGML.pl in the location

macros/PGML.pl

Also, you want to extract scalars (prefix $) from arrays (prefix @), so use BEGIN_PGML [$flock[$i] ] END_PGML but do not use BEGIN_PGML [ @flock[$i] ]
END_PGML

Or, just use the solution that recommends hashes.

### Re: randomizing word pairs in question text.

by Alex Jordan -
Another option is to use hashes, which are like functions with input and output (keys and values). Depending on taste, the code may be more to your liking.

$bird = list_random('goose', 'crow', 'starling'); %plural = (goose=>'geese', crow=>'crows', starling=>'starlings'); %flock= (goose=>'gaggle', crow=>'murder', starling=>'murmuring'); BEGIN_TEXT The plural of$bird is $plural{$bird}. A group of $plural{$bird} is called a $flock{$bird}.
END_TEXT

by Alex Jordan -

BEGIN_PGML
The plural of [$bird] is [$plural{$bird}]. A group of [$plural{$bird}] is called a [$flock{$bird}]. END_PGML In reply to Alex Jordan ### Re: randomizing word pairs in question text. by tim Payer - Thank you all but unfortunately I am not having any success with my flock of bird word pairs. Could you please help? Only the single instance of [$bird] is rendering. Everything else is not rendering.

I tried "hashes" as an alternative form and I tried indexing.
And no, I do not see where I can load the latest version of PGML through my file Manager.

I am using the "Free Trial" version 10 that was distributed last July.
As the hashes do NOT seem to work, must I get a newer version?

Thanks so much for your help.

The code block is below if you can see anything that I have missed.

# DESCRIPTION   Function translations
# Translate descriptions of critical points and function
# into correct notation.
# WeBWorK problem written by TimPayer <tsp1@humboldt.edu>
# ENDDESCRIPTION

## DBsubject(Probability)
## DBchapter(Random variables)
## DBsection(Expectation)
## Institution(Humboldt State University)
## Author(Tim Payer)
## KEYWORDS(probability, translate, notation)
DOCUMENT();

"PGstandard.pl",
"PGunion.pl",
"MathObjects.pl",
"parserPopUp.pl",
"PGML.pl",
"PGcourse.pl",
);

#Text(beginproblem());   #uncomment

$showPartialCorrectAnswers = 1; Context("Numeric"); # create three arrays of the same length "in parallel" # Using "hashes":$bird = list_random('goose', 'crow', 'starling');
%plural = (goose=>'geese', crow=>'crows', starling=>'starlings');
%flock= (goose=>'gaggle', crow=>'murder', starling=>'murmuring');

#@bird = ('goose','crow','starling');  ##Apparently I do not have the latest PGML?
#@birds = ('geese','crows','starlings');
#@flock = ('gaggle','murder','murmuring');
#$i = random(0, 2, 1); # choose an array index at random$popup1 = PopUp(
["probability notation", "P(V = 0)", "P(V = 1)", "P(V = 2)", "P(V  > 0)", "P(V  > 1)","P(V < 2)",], "P(V = 2)");

$popup2 = PopUp( ["probability notation", "P(V = 0)", "P(V = 1)", "P(V = 2)", "P(V > 0)", "P(V > 1)","P(V < 2)",], "P(V > 0)");$popup3 = PopUp(
["probability notation", "P(V = 0)", "P(V = 1)", "P(V = 2)", "P(V = 3)","P(V  > 0)", "P(V > 1)","P(V < 2)","P(V < 3)",], "P(V  > 0)");

# Need if then for 1c depending on whether "at least one or at most 2

BEGIN_PGML

Trials to test:

The plural of [$bird] is [$plural{$bird}]. A group of [$plural{$bird}] is called a [$flock{$bird}]. 1. A flock of [$plural{$bird}] are described as a [$flock{$bird} ] of [$plural].
Consider the case where (percent)% of a [$flock[$i] ] of [$birds[$i] ] carry a bird flu virus, and that two [$birds[$i]] are drawn at random from the [$flock[$i] ], find the following probabilities but first complete the following event variable declaration:

Let V = The number of [$birds[$i] ] that have the bird flu virus from (two $k) random draws of the [$flock[$i] ]. 1a.) What is the probability of randomly drawing two [$birds[$i] ] that both have the bird flu virus? Choose the correct probability notation below and then calculate its associated probability with fourth decimal accuracy. [___]{$popup1}  =

1b.) What is the probability of randomly drawing two [$birds[$i] ] such that at least one [$bird[$i]] has the bird flu virus? Choose the correct probability notation below and then calculate its associated probability with fourth decimal accuracy.

[___]{$popup2} = 1c.) Consider the case where three [$birds[$i]] are drawn at random from the [$flock[$i] ]. What is the probability of drawing (draw$j at least one [$bird[$i] ] that has , at most two [$birds[$i] ]  that have ) the bird flu virus? First complete the declaration of the event variable below:

Let V = The number of [$birds[$i] ] that have the bird flu virus from (three $k) random draws of the [$flock[$i] ]. Choose the correct probability notation below and then calculate its associated probability with fourth decimal accuracy. [___]{$popup3} =

END_PGML

BEGIN_PGML_SOLUTION

END_PGML_SOLUTION

ENDDOCUMENT();

### Re: randomizing word pairs in question text.

by Alex Jordan -
The following (cut from your sample) works for me. But perhaps you have an older version of pgml.pl that doesn't parse things like [$flock{$bird}] correctly. I'm using 2.11. I think I have a few development branch pulls as well, and I can't say at the moment if pgml.pl is affected. Be wary of just acquiring the latest version of pgml.pl without a full upgrade, since it's possible that concurrent upgrades to deeper parts of the engine will be required for the newer pgml.pl.

Maybe if you have to stick with your current version, [@$flock{$bird}@] will work. I'm not sure though.

$bird = list_random('goose', 'crow', 'starling'); %plural = (goose=>'geese', crow=>'crows', starling=>'starlings'); %flock= (goose=>'gaggle', crow=>'murder', starling=>'murmuring'); BEGIN_PGML The plural of [$bird] is [$plural{$bird}]. A group of [$plural{$bird}] is called a [$flock{$bird}].

END_PGML