SGI

subtractive_rng

Category: functors Component type: type

Description

Subtractive_rng is a Random Number Generator based on the subtractive method [1]. It is a Unary Function: it takes a single argument N, an unsigned int, and returns an unsigned int that is less than N. Successive calls to the same subtractive_rng object [2] yield a pseudo-random sequence.

Example

int main()
{
  subtractive_rng R;
  for (int i = 0; i < 20; ++i)
    cout << R(5) << ' ';
  cout << endl;
}
// The output is   3 2 3 2 4 3 1 1 2 2 0 3 4 4 4 4 2 1 0 0 

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h. This function object is an SGI extension; it is not part of the C++ standard.

Template parameters

None.

Model of

Random Number Generator, Adaptable Unary Function

Type requirements

None.

Public base classes

unary_function<unsigned int, unsigned int>

Members

Parameter Description Default
argument_type Adaptable Unary Function The type of a subtractive_rng's argument: unsigned int.
result_type Adaptable Unary Function The type of the result: unsigned int.
subtractive_rng(unsigned int seed) subtractive_rng See below.
subtractive_rng() subtractive_rng See below.
unsigned int operator()(unsigned int N) Adaptable Unary Function Function call. Returns a pseudo-random number in the range [0, N).
void initialize(unsigned int seed) subtractive_rng See below.

New members

These members are not defined in the Adaptable Unary Function requirements, but are specific to subtractive_rng.
Member Description
subtractive_rng(unsigned int seed) The constructor. Creates a subtractive_rng whose internal state is initialized using seed.
subtractive_rng() The default constructor. Creates a subtractive_rng initialized using a default value.
void initialize(unsigned int seed) Re-initializes the internal state of the subtractive_rng, using the value seed.

Notes

[1] See section 3.6 of Knuth for an implementation of the subtractive method in FORTRAN. Section 3.2.2 of Knuth analyzes this class of algorithms. (D. E. Knuth, The Art of Computer Programming. Volume 2: Seminumerical Algorithms, second edition. Addison-Wesley, 1981.)

[2] Note that the sequence produced by a subtractive_rng is completely deterministic, and that the sequences produced by two different subtractive_rng objects are independent of each other. That is: if R1 is a subtractive_rng, then the values returned when R1 is called depend only on R1's seed and on the number of times that R1 has been called. Calls to other subtractive_rng objects are irrelevant. In implementation terms, this is because the class subtractive_rng contains no static members.

See also

Random Number Generator
[Silicon Surf] [STL Home]
Copyright © 1999 Silicon Graphics, Inc. All Rights Reserved. TrademarkInformation