## PREP 2013 Question Authoring - Archived

### Factors of a number

by Joyce Eveland -
Number of replies: 5

I want to assign random numbers and then choose only those where 360 mod random number ==0. Is mod a valid operator?

I have tried the following

Context("Numeric");

$e=random(10,60,1); #assigns measure of exterior angle #$n=Compute("360/$e"); #finds number of sides of polygon #not all values will give a regular polygon. Must be factors of 360. if (360%$e==0) { $e =$e;}
else {$e=$s+1;}

When I try problem my random number is always 1.

### Re: Factors of a number

by John Travis -
Not at a computer right now but quickly noticed that you have a typo in your increment.  Don't you mean

$e =$e+1;

Also don't you want this to keep going until you get a match?

To be honest it might be easier to just list all of the desired factors of 360 in a list and then randomly select one.

### Re: Factors of a number

by Joyce Eveland -
That is what I thought when I woke up this morning! Just list the numbers.

### Re: Factors of a number

by Paul Pearson -
Hi Joyce,

First, a plug: I would like to encourage everyone to post their entire source code for a problem (i.e., all of the contents of a .pg file) so that we can give your code a test run and spot any mistakes easily.

If you want to select random numbers from a list, the way to do that in WeBWorK is to use the list_random() command. The random() command is

$a = random(start, stop, step_size); whereas the list_random() command is$a = list_random( number_1, number_2, number_3, number_4, ...);

So, if you want a list of some numbers between 10 and 60 that divide 360 with zero remainder, use

$a = list_random(10, 12, 15, 18, 20, 24, 30, 36, 40, 45, 60); and then the value of the scalar$a will be one of the eleven numbers listed.

Good luck!

Paul Pearson

### Re: Factors of a number

by Gavin LaRose -
Hi all,

As a side note to follow-up with Paul's comment, I believe that list_random will take strings as arguments, too. Thus one could use something like the following to select from a set of words to use in a problem.

  ...
$animal = list_random( 'cat', 'dog', 'mouse', 'elephant' );$animalSelect = PopUp( [ '?', 'cat', 'dog', 'mouse', 'elephant' ], $animal ); BEGIN_TEXT The randomly selected animal I'm thinking of is \{$animalSelect->menu() \}
END_TEXT
ANS( \$animalSelect->cmp() )
...


Perhaps obviously, this particular example wouldn't be the most enlightening problem for a student to try and answer.

Gavin

Yes, list_random does accept strings, so your example works fine. This is also another way to handle Mary's question about using a randomly chosen variable name.