Thread overview
Experimental approach to reference-type random number generators
Aug 24, 2013
bearophile
August 24, 2013
Hello all,

Today I spent some time playing around with an attempt at a wrapper for random number generation.  The idea is that random number generators should exist as reference types, but without needing to be implemented as classes.

The basic wrapper is here: http://codepad.org/AlhLJa6A

And a partial port of std.random using the new wrapper is here:
http://codepad.org/SEBYMns6

See e.g. lines 392-394 and 666-670 for how the wrapper interacts with the random number engines.

The basic idea seems to work, and with minimal overhead (it may even be slightly faster in some scenarios).  A few of the unittests have had to be commented out (e.g. an opEquals needs to be implemented for the wrapper).

It looks promising to me, so before I go further I thought I'd open it up for review and commentary.

Some of the features, e.g. the need for the engine to be initialized, may explain some of my earlier questions on the mailing lists today ... :-)

I do have one specific query, about whether the way that .save is implemented for the wrapper could leak memory given the fact that RefCounted is passed the template parameter RefCountedAutoInitialize.yes.

Apart from that, {enj, destr}oy.  Does this look like a valid route to a next-gen std.random? :-)

Best wishes,

    -- Joe
August 24, 2013
Joseph Rushton Wakeling:

> Today I spent some time playing around with an attempt at a wrapper for random number generation.  The idea is that random number generators should exist as reference types, but without needing to be implemented as classes.

I think I'd like std.random2.Random to be a final class.

Bye,
bearophile
August 24, 2013
On Saturday, 24 August 2013 at 19:42:18 UTC, bearophile wrote:
> I think I'd like std.random2.Random to be a final class.

That was also an approach I considered.  It's easy -- even trivial -- to do.  But since Phobos has such a strong struct-oriented approach, I thought I'd try something that could make that work.

One of the benefits of that approach is that it should be possible to drop std.random2 into 99% of code and have it Just Work, without downstreams having to change a line of their own code.