WeBWorK Main Forum

loading problem

loading problem

by Adam Z -
Number of replies: 3

Hello all: 

I am beginner at using this , this code takes long time to load , any idea of fixing this up:


DOCUMENT();        # This should be the first executable line in the problem.


loadMacros(

   "PGstandard.pl",     # Standard macros for PG language

   "MathObjects.pl",

   "PGML.pl",

   "contextCurrency.pl",

   "PGcourse.pl",

);


TEXT(beginproblem());

Context("Currency");

$showPartialCorrectAnswers = 1;

 

do{$a = Currency(random(15000,30000,5));

$b = Currency(random(20000,40000,5));

$c = Currency(random(50,300,5));

$d = Currency(random(20,100,5));

$y = random(1,1000,1);

$m=$a+$c*$y;

$n=$b+$d*$y;

} until ($m==$n);


@r = ('Mae','Suzan', 'Donna');

$r_index = random(0,2,1);

$r = $r[$r_index];

BEGIN_PGML

[$r] currently sells televisions for company A at a salary of [$a] plus a [$c] commission for each television she sells. Company B offers her a position with a salary of [$b] plus a [$d] commission for each television she sells. How many televisions would [$r] need to sell for the options to be equal?

[$r] needs to sell [________]{$y} televisions.



END_PGML


ENDDOCUMENT();       # This should be the last executable line in the problem.

In reply to Adam Z

Re: loading problem

by Danny Glin -
On average your loop is going to require a very large number if iterations before it finds five values that satisfy the condition.

A much more efficient way to do this would be to randomly generate four of the five parameters, and then assign a value to the fifth parameter so that your desired condition is satisfied.

In your case you want $a+$c*$y to equal $b+$d*$y, so you could let $b=$a+($c-$d)*$y; You would just have to make sure that you always end up with a positive value of $b.

One option would be something like

$a = Currency(random(15000,30000,5));

$d = Currency(random(20,100,5));

$c = Currency(random($d+5,300,5));

$y = random(1,1000,1);

$b = $a + ($c-$d)*$y;

This ensures that $c>$d, so the value of $a + ($c-$d)*$y will always be positive.
In reply to Danny Glin

Re: loading problem

by Adam Z -

Thank you, Danny. Very helpful. I will implement this and see how it goes. One more question, in another question I assign  $x= Currency($a). But the output does not accept decimals how to sort out this issue and how to generate numbers with tenths only but written in hundredths e.g. 10.3 but need to be shown as 10.30 and so on. 

In reply to Adam Z

Re: loading problem

by Danny Glin -
Can you post a full code snippet, along with what you're seeing, and what the desired output is?