Boost logo

Boost Users :

Subject: Re: [Boost-users] [random] Singleton engine
From: Kevin Martin (kev82_at_[hidden])
Date: 2009-07-29 03:17:45


On 29 Jul 2009, at 04:47, Diederick C. Niehorster wrote:
>
> My code has now become what is pasted below. It does the trick and
> allows for singletons with a parameterized constructor.

I still don't see what your CRandom class offers over creating a
variate_generator and putting it in a boost::function, if you are not
familiar with the function library you should look it up, it's
brilliant.

More importantly though, I'm not sure you are thinking about your
problem in the right way. How do you want to use your random number
generator code? What classes as misuse and what kind of misuses do you
want to warn the user about? For me the most important issue was
forgetting to seed the generator, so that's why mine is coded that way.

I think it may be worth asking yourself some of these questions, see
if they are important to you and think how you current implementation
would deal with them.

1) Do I want random numbers to be generated before main() has been
called, do I want the code to stop me from doing this accidently?
2) Will I ever want to create random variables (CRandom<> objects)
before main has been called?
3) Will I ever want to use a random variable which doesn't use the
singleton instance as it's source of randomness, do I still want such
a random generator to plug in with the rest of my code in a unified way?
4) Will I ever be using the generator in a multithreaded environment?
Do I need it to always supply the same numbers to the same thread?

Finally, I would also be very careful with your Singleton class,
because I think (although I haven't checked) that Instance(),
Instance(unsigned int), and Instance(unsigned int(*)()) will all
successfully instantiate and all return different instances.

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