Adobe Systems Incorporated


Category : functors Component type: type


Subtractive_rng is a RandomNumberGenerator based on the subtractive method [1]. It is a UnaryFunction : 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.


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 


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


Model of

RandomNumberGenerator, AdaptableUnaryFunction

Type requirements


Public base classes

unary_function<unsigned int, unsigned int>


Parameter Description Default
argument_type AdaptableUnaryFunction The type of a subtractive_rng's argument : unsigned int.
result_type AdaptableUnaryFunction 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) AdaptableUnaryFunction 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 AdaptableUnaryFunction 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.


[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


Copyright © 2006-2007 Adobe Systems Incorporated.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.

Search powered by Google