On Tuesday 08 July 2003 08:01 am, Neal D. Becker wrote:
> 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.

After some reflection, I think a somewhat more generalized version might be 
even better.  This one can produce sequences of n-bit rather than only 1-bit.

I'm not sure what to do with max_value and min_value.  It depends on 
interpretation- whether you intend to sign extend the values and interpret 
them as signed or zero extend and interpret as unsigned.

#ifndef pnseq_generator_H
#define pnseq_generator_H

#include <boost/random.hpp>

namespace boost
{
  namespace random
  {

    template < class RandomNumberGenerator, int width=1 >
    class pnseq_generator
    {

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

      BOOST_STATIC_CONSTANT (bool, has_fixed_range = true);
      BOOST_STATIC_CONSTANT (result_type, min_value = 0);
      BOOST_STATIC_CONSTANT (unsigned int, max_value = ~(unsigned(-1) << 
width));
      BOOST_STATIC_CONSTANT (int, mask = ~(unsigned(-1) << width));

      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()()
      {
        int const  bits = cache_ & mask;
        cache_ >>= width;
        if ( --count_ <= width-1 )
          init();
        return bits;
      }

      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

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Reply via email to