in reply to Create math homework problems

This may have been intentional, but I noted a gotcha in this line:     $num=int(rand $#arr); That will never pick the last index in @arr (as long as there is more than one element in @arr); 10 * 10 will always be the last question asked.

A quick example: With four elements in @arr, $#arr is 3. rand(3) will return a number >= 0 and < 3. When you truncate the result with int(), you get 0, 1, or 2.

I would have written that line as:     $num=int(rand @arr);

Replies are listed 'Best First'.
Re: Re: Create math homework problems
by t0mas (Priest) on Jan 10, 2001 at 12:29 UTC
    I know that it leaves 10 times 10 as the last question and I thought leaving it as a 'grande finale' was kind of nice. I first considered some algorithm that favoured the lower numbers and there by increased the difficulty at the end of the program (my son thinks larger numbers are more difficult). But this was just a 5 minute hack, so i didn't put that much effort into it.

    I'll change the comment in the code to explain this behaviour. Thanks for your explanation.

    /brother t0mas