next up previous contents index
Next: 20.1.0.0.2 Seeding the random Up: 20.1 Random Number Generation Previous: 20.1 Random Number Generation

20.1.0.0.1 C++ Support

This random number generator is implemented by the RNG class, defined in rng.h:
class RNG : public TclObject {
enum RNGSources { RAW_SEED_SOURCE, PREDEF_SEED_SOURCE, HEURISTIC_SEED_SOURCE };
        ...
        // These are primitive but maybe useful.
        inline int uniform_positive_int() {  // range [0, MAXINT]
                return (int)(stream_.next());
        }
        inline double uniform_double() { // range [0.0, 1.0)
                return stream_.next_double();
        }

        inline int uniform(int k)
                { return (uniform_positive_int() % (unsigned)k); }
        inline double uniform(double r) 
                { return (r * uniform_double());}
        inline double uniform(double a, double b)
                { return (a + uniform(b - a)); }
        inline double exponential()
                { return (-log(uniform_double())); }
        inline double exponential(double r)
                { return (r * exponential());}
        inline double pareto(double scale, double shape)
                { return (scale * (1.0/pow(uniform_double(), 1.0/shape)));}
        ...
};

The uniform_positive_int method generates random integers in the range [0,231-1]. In particular, Additional member functions provide the following random variate generation:



The Random class (in random.h) is an older interface to the standard random number stream.

Here's a sample use of RNG modeled on RED. rng_ is an instance of class RNG:

        \ldots
        // drop probability is computed, pick random number and act
        double u = rng_-\>uniform_double();
        if (u \<= edv_.v_prob) {
                edv_.count = 0;
                if (edp_.setbit) 
                        iph-\>flags() |= IP_ECN; /* ip ecn bit /
                else
                        return (1);
        }
        \ldots




2000-08-24