Random Function

This demonstrates how to get a random function.

Complete Code

Download file: RandomFunction.pg

PG problem file

Explanation

DOCUMENT();
loadMacros('PGstandard.pl', 'PGML.pl', 'PGcourse.pl');

Preamble

These standard macros need to be loaded.
# Define some random values and functions
$a = non_zero_random(-8, 8);
$b = random(1, 8);
$n = random(2, 4);

@funs = (
    "1 + $a*x + $b x^2",
    "$a / (1 + $b x)",
    "$a x^3 + $b",
    "($a - x) / ($b + x^2)",
    "cos($n*x)"
);

# This select one of the functions at random.
$f = Formula($funs[ random(0, $#funs) ])->reduce;

Setup

First, there are some random numbers generated as well as an array of functions using those values. The statement random(0,$#funs) generates a random number between 0 and (in this case 4, but in general 1 less than the length of the array) and then that element of the array is selected.

BEGIN_PGML
Enter [``[$f]``] [____]{$f}
END_PGML

Statement

This is the problem statement in PGML.
BEGIN_PGML_SOLUTION
Solution explanation goes here.
END_PGML_SOLUTION

ENDDOCUMENT();

Solution

A solution should be provided here.