C++ wrapper for gmp_randstate_t

Alexey Beshenov bav.272304 at gmail.com
Thu May 10 00:30:03 CEST 2007


Why there is no C++ wrapper for gmp_randstate_t in GMP?

The following code is taken from my MPFRCPP library 
(http://www.nongnu.org/mpfrcpp/):

============================================================
class RandomState {
        gmp_randstate_t rs_;
public:
    /// Mersenne Twister algorithm
    RandomState () throw() {
        gmp_randinit_mt (rs_);
    }

    /// Copy
    RandomState (const RandomState& rs) throw() {
        gmp_randinit_set(rs_, rs.rs_);
    }
    /// Copy
    RandomState (const gmp_randstate_t& rs) throw() {
        gmp_randinit_set(rs_, rs);
    }

    /// Linear congruental algorithm X = (aX+c) mod 2^{m2exp}
    RandomState (const mpz_class& a,
                 unsigned long int c,
                 unsigned long int m2exp) throw() {
        gmp_randinit_lc_2exp (rs_, a.get_mpz_t(), c, m2exp);
    }

    /// Linear congruental algorithm with data selected from a table
    RandomState (unsigned long int size) throw() {
        gmp_randinit_lc_2exp_size (rs_, size);
    }

    /// Copy
    RandomState& operator= (const RandomState& rs) throw() {
        gmp_randinit_set(rs_, rs.rs_);
        return *this;
    }

    gmp_randstate_t& getGmpRandstateT () throw() {
        return rs_;
    }

    const gmp_randstate_t& getGmpRandstateT () const throw() {
        return rs_;
    }

    void seed (const mpz_class& seed) throw() {
        gmp_randseed (rs_, seed.get_mpz_t());
    }

    void seed (unsigned long int seed) throw() {
        gmp_randseed_ui (rs_, seed);
    }

    ~RandomState () throw() {
        gmp_randclear (rs_);
    }
};
============================================================

I think it could be useful.

-- 
Alexey Beshenov <bav.272304 at gmail.com>
+7 960 15 30 767 / +7 950 80 300 18


More information about the gmp-discuss mailing list