1st draft of complete class-based std.random successor
Joseph Rushton Wakeling
joseph.wakeling at webdrake.net
Sun Mar 23 02:59:37 PDT 2014
On Saturday, 22 March 2014 at 23:56:35 UTC, bearophile wrote:
> They seem good.
Excellent!
There may need to be some attention to the internals of
uniform01. Its correctness depends on whether one can always
trust a float-based RNG to return values in [min, max) or whether
[min, max] is also going to be supplied by some.
> More ideas:
>
> "Three suggestions for std.random":
> https://d.puremagic.com/issues/show_bug.cgi?id=4851
I think all std.random functions now support a default RNG.
There were some bugs related to that (e.g. the "can't use
Xorshift" one) that I fixed last year.
The problem you identify with,
int r = randomCover(data, rndGen).front;
always returning the same value, is down to the fact that rndGen
is being copied inside the RandomCover struct by value, so of
course the original rndGen is never updated and each of these
calls will produce the same result. The new std.random2 fixes
that, because the RNGs are reference types.
However, I'd have thought that
int r = data.sample(1, rndGen).front;
would have been a more efficient way to implement "choice", as it
can operate on any input range, as long as it has the .length
property; and it ought to be _much_ faster than even a single
call to randomCover.
One could always use this as a default option, with a
specialization where data is a RandomAccessRange to use the more
efficient
int r = data[uniform!"[)"(0, data.length)];
> "Strongly pure random generator":
> https://d.puremagic.com/issues/show_bug.cgi?id=5249
.front and .popFront at least are pure for _all_ the RNGs
currently implemented in std.random2.generator. See e.g.:
https://github.com/WebDrake/std.random2/blob/master/std/random2/generator.d#L266-L272
https://github.com/WebDrake/std.random2/blob/master/std/random2/generator.d#L506-L517
https://github.com/WebDrake/std.random2/blob/master/std/random2/generator.d#L821-L834
Of course this is not strongly pure in line with your request,
but it should enable use of these RNGs in many other scenarios
where purity is important.
> I hope a gaussian (normal distribution) generator is planned or
> present.
https://github.com/WebDrake/std.random2/blob/master/std/random2/distribution.d#L326
This is a range implementation; there will also be a function
implementation, which will probably follow the inefficient
Box-Muller variant that uses 2 uniform random variates to
generate a single normal variate (as per the example you posted
in your feature request).
More information about the Digitalmars-d-announce
mailing list