PGrandom

From WeBWorK_wiki
Jump to navigation Jump to search

PGrandom

Description

A small object which generates a random number.

Syntax

$rand_num_generator = new PGrandom($initial_seed);
# creates generator with a fixed initial seed

$a = $rand_num_generator->random(-10,10,0.1);
# generates random number between -10 and 10 in 0.1 increments.

Attributes

Name Description Defaults
seed Last internal random number generated.

Methods

Method Action/Returns Other
random($initial, $final, $increment) Generates a random number between the initial and final values in steps of increment. If the increment is 0 or less, then random chooses a floating point number between $initial and $final using a small increment. $increment defaults to 1, if it is not supplied as an argument.
rand( $final) Behaves like perl's rand. Generates a random number between 0 and $final. $final defaults to 1 if that argument is omitted.
seed($new_seed) or
srand($new_seed)
Resets the seed for this random number generator object.

Notes

Using independent random number generators in a complicated problem or macro promotes compartmentalization and robustness. If all macros were to use a common random number generator, then a change in the number of calls to the random number generator in one macro would affect the results in another macro, causing confusion and a difficult debugging process.
random() itself is defined using a random number generator object which is stored in the global variable $main::PG_random_generator