Boost logo

Boost :

From: Neal D. Becker (nbecker_at_[hidden])
Date: 2003-07-08 07:01:24


On Monday 07 July 2003 05:06 pm, Jens Maurer wrote:
> I've updated the current Boost.Random CVS to the interface
> contained in the C++ library TR proposal:
>
> http://std.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1452.html
>
> The boost documentation has not yet been updated, I hope to be able
> to do that later this week. Reading the TR proposal should give
> you a sufficient idea.
>
> There have been various suggestions for changes from C++
> committee members, so additional (but minor) interface changes
> may happen after the October 2003 meeting.
>

I would also like to suggest one addition that is very useful. It is very
common in communications to need a generator that produces a 1-bit sequence.

#ifndef pnseq_generator_H
#define pnseq_generator_H

#include <boost/random.hpp>

namespace boost
{
  namespace random
  {

    template < class RandomNumberGenerator >
    class pnseq_generator
    {

    public:
      typedef pnseq_generator<RandomNumberGenerator> self_type;
      typedef RandomNumberGenerator base_type;
      typedef bool result_type;

      BOOST_STATIC_CONSTANT( bool, has_fixed_range = true );
      BOOST_STATIC_CONSTANT( result_type, min_value = false );
      BOOST_STATIC_CONSTANT( result_type, max_value = true );

      result_type min() const
      { return min_value; }
      result_type max() const
      { return max_value; }

      explicit pnseq_generator (base_type & rng)
        : rng_( rng ), count_()
      { init(); }

      template < typename T >
      void seed( T s )
      { rng_.seed( s ); init(); }

      result_type operator()()
      {
        bool const bit = cache_ & 1;
        cache_ >>= 1;
        if ( --count_ <= 0 )
          init();
        return bit;
      }

      bool validation( result_type x ) const
      { return valid == x; }

      friend bool operator ==( self_type const &x, self_type const &y )
      {
        return x.rng_ == y.rng_ && x.cache_ == y.cache_
          && x.count_ == y.count_;
      }
      friend bool operator !=( self_type const &x, self_type const &y )
      { return !(x == y); }

    private:
      typedef typename base_type::result_type cache_type;

      void init()
      {
        cache_ = rng_();
        count_ = std::numeric_limits<cache_type>::digits;
      }

      base_type& rng_;
      cache_type cache_;
      int count_;

    }; // boost::random::pnseq_generator

  } // random
} // boost

#endif


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk