1 // Boost min_rand.hpp header file ------------------------------------------//
3 // (C) Copyright Beman Dawes 1998. Permission to copy, use, modify, sell and
4 // distribute this software is granted provided this copyright notice appears
5 // in all copies. This software is provided "as is" without express or implied
6 // warranty, and with no claim as to its suitability for any purpose.
8 // Version 1.1, 25 May 99 Add operator()() to meet Generator requirements
9 // Version 1.0, 9 Nov 98 Initial version
11 #ifndef BOOST_MIN_RAND_HPP
12 #define BOOST_MIN_RAND_HPP
18 // min_rand ----------------------------------------------------------------//
20 // see min_rand.html for documentation
24 // Don't even think about changing the values of the constants below.
25 // See the article cited in the documentation for rationale.
27 modulus = 2147483647L,
28 multiplier = 48271L, // 16807L for original "minimal standard"
29 validation = 399268537L, // 1043618065L for original "minimal standard"
30 q = modulus / multiplier,
31 r = modulus % multiplier
34 long value; // invariant: 0 < value <= modulus
38 // compiler generated copy constructor and operator= are valid and useful
40 explicit min_rand( long seed_value=1 ) : value( seed_value )
41 { assert( value > 0 && value <= modulus ); }
43 operator long() const { return value; }
44 double fvalue() const { return double(value) / modulus; }
46 min_rand& operator=( long new_value ) {
48 assert( value > 0 && value <= modulus );
52 long operator++() { value = multiplier*(value%q) - r*(value/q);
53 if ( value <= 0 ) value += modulus;
54 assert( value > 0 && value <= modulus );
57 long operator++(int) { long temp = value; operator++(); return temp; }
59 long ten_thousandth() const { return validation; }
61 // satisfy std::RandomNumberGenerator and std::Generator requirements:
62 typedef long argument_type;
63 typedef long result_type;
64 long operator()( long n ) { return operator++() % n; }
65 long operator()() { return operator++(); }
71 #endif // BOOST_MIN_RAND_HPP