Boost logo

Boost Users :

Subject: Re: [Boost-users] [random] Singleton engine
From: Kevin Martin (kev82_at_[hidden])
Date: 2009-08-17 03:25:40


On 17 Aug 2009, at 04:51, Diederick C. Niehorster wrote:

> I have decided to try to do without the whole singleton (although I
> don't really see what the point is of avoiding a singleton if we still
> have a global pointer, its almost the same thing) as an exercise and
> try out Kevin's implementation.

A singleton is extra code and extra work to maintain. Why create
yourself more classes, more namespace pollution, and a more complex
system when it is not necessary.

I think I am just anti singleton. I believe it is far too overused,
and apart from in a few niche cases provides nothing but additional
complexity.

> Hmm, i have tried so, but the syntax to actually extract the random
> numbers becomes very complicated now (I think i am missing a point
> here, i am new to boost.function).

Your method 2 is the one I use (except I use =) I wouldn't say it is a
lot of typing, or particularly complicated, most editors will let you
autocomplete the grng_function and the distribution name, so it's only
about 15 keystrokes. In fact it is no different to

CRandom<boost::uniform_int<int>> Cr(1,100);

If you really wanted you could define a macro to make the function
call for you, kind of equivalent to your typedef. I think that would
make it harder to read though and not easier, as you would need a
macro for each distribution.

The reason I like it because I find it very readable and it only adds
two symbols to the namespace. Take this code for example, if you had a
separate wrapper class for the two distributions, would it be easier
to read?

(Note I use the old syntax for boost::function because I don't like
the new one)

boost::shared_ptr<boost::multi_array<int, 2> > vals =
  fbc_extractCellValues(board);
MatrixSwap ms(*vals);

boost::function0<bool> RVswapRow = grng_randomVariable(
  boost::bernoulli_distribution<>());
boost::function0<int> RVindex = grng_randomVariable(
  boost::uniform_smallint<>(0, board.boardSize()-1));

for(int i=0;i!=10000;++i) {
     int idx1 = 0, idx2 = 0;
     while(idx1 == idx2) {
         idx1 = RVindex();
         idx2 = RVindex();
     }
     if(RVswapRow()) {
         ms.swapRows(idx1, idx2);
     } else {
         ms.swapCols(idx1, idx2);
     }
}

Thanks,
Kevin Martin


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net