Sampling algorithms for D
Dmitry Olshansky
dmitry.olsh at gmail.com
Thu Apr 12 08:58:23 PDT 2012
On 12.04.2012 18:45, Joseph Rushton Wakeling wrote:
> Hello all,
Hello there,
[snip]
>
> So, introduction away, here are the main questions I came up with in
> creating the code.
>
> (1) Effective handling of random numbers.
> ----
> The design concept was for the sampler classes to use a specified
> uniform random number generator if provided, otherwise to use rndGen.
> This turned out to be trickier than anticipated, and I'm not very happy
> with the current solution.
>
> My first attempt just templatized class methods select() and skip().
> However, one of the algorithms (class VitterD) requires random
> initialization as well, and trying to template this() ran into this bug:
> http://d.puremagic.com/issues/show_bug.cgi?id=435
>
> I'm really not too happy with turning the whole class into a template
> just for the sake of the random number generator, so can anyone suggest
> an effective way to resolve the problem?
>
> (I may be biased here by C/GSL experience, where I've got used to
> passing the RNG as a pointer, meaning runtime polymorphism.)
I think that if you like function pointer thing, you'd love D's
delegates and lambdas. If templates are too pervasive it might be better
just use little bit of dynamic stuff.
e.g.
Mt19937 gen;
double delegate() rng = (){
auto x = gen.front;
gen.popFron();
return x;
}
use it somewhere later on:
writeln(rng(), rng()); // print 2 random numbers
>
> (2) init() function?
> ----
> I was unsure whether it would be good design practice for the sampler
> classes to be throwaway (1 instance = 1 use) or to put in place an
> init() function that would reset it with new record and sample sizes.
> Any thoughts?
Use structs? They are cheap throwway value types allocated on stack (by
default).
>
> (3) Uniform random number on (0,1)
> ----
> The algorithms' specification explicitly refers to uniform random
> numbers on the open interval, which I take to mean (0,1) i.e. excluding
> zero. Phobos currently provides only a half-open uniform distribution on
> [a,b).
>
> I've implemented a simple uniform_open() function which should return a
> uniform distribution on (a,b). The question is, are there any plans for
> an open-interval uniform distribution in Phobos? Can I rely on this
> functionality being provided in the long term in D's standard library?
>
> (4) Truncation
> ----
> The code uses C's trunc() function to return the integer part of a
> floating point number. D doesn't seem to like it very much when I then
> set a size_t as equal to the result of trunc(), and I have to use a type
> cast. Is there a more elegant D-ish way of achieving the same result?
> roundTo() isn't adequate, as it rounds to the _nearest_ integer rather
> than the integer part.
>
It's OK. there are no implicit conversions of FP--> integer in the
language I think.
> (5) ... any general stylistic comments? :-)
>
Looks fine ;)
> Thanks and best wishes,
>
> -- Joe
--
Dmitry Olshansky
More information about the Digitalmars-d-learn
mailing list