Forum archive 2000-2006

Michael Gage - PGrandom

Michael Gage - PGrandom

by Arnold Pizer -
Number of replies: 0
inactiveTopicPGrandom topic started 8/28/2001; 8:30:28 PM
last post 8/28/2001; 8:30:28 PM
userMichael Gage - PGrandom  blueArrow
8/28/2001; 8:30:28 PM (reads: 2976, responses: 0)

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

NameDescriptionDefaults
seedLast internal random number generated.  

Methods

MethodAction/ReturnsOther
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

See Also

"random"

 

<| Post or View Comments |>