Normal/Gaussian random number generation for D

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Sun Nov 11 05:05:25 PST 2012


On 11/06/2012 07:14 PM, jerro wrote:
> In general I like the API in file normal.d attached to your original post. I
> think the engines should have an option to do some initialization in Normal's
> constructor, though. We could achieve that by calling _engine.initialize in
> Normal's constructors, if such method exists.

What's wrong with such initialization being in the constructor of the relevant 
NormalEngine?  I think that was your original idea, and I derailed it because of 
my misunderstanding of what you wanted to initialize.

> The users would need to construct their own instance of engine to use the
> function that takes engine as a parameter. So it would make sense to add helper
> functions for creating engine instances.

In general the idea is that the engine should be something hidden; where you 
need to use it, you just need to pass the name as a template parameter; it 
should be rare that you really need to manually instantiate your own engine. 
But we can add such a helper function if you like.

What I'm frustrated about is that as-is it's not possible to just have

     auto nrng = Normal(mean, sigma);

... but you have instead to write,

     auto nrng = Normal!()(mean, sigma);

despite the fact that Normal has default template parameters given.  So maybe a 
helper function normalRNG which returns an instance of Normal would also be helpful.

> There's one change that I think would make the API more convenient. Normal
> struct and the engine don't store an instance of a RNG , so they don't need to
> take it as a template parameter. We could make opCall methods templates instead.
> That way the users would never need to explicitly specify the type of the RNG.

Good call.

I've uploaded a tweaked version based on your comments here:
https://github.com/WebDrake/phobos/tree/normal

... so feel free to pull, further revise and add in your Ziggurat implementation :-)

I haven't yet written any of the helper functions you suggest, but will do so 
shortly based on your response to my above remarks.


More information about the Digitalmars-d mailing list