Sampling algorithms for D

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Thu Apr 12 15:57:03 PDT 2012


On 13/04/12 00:48, bearophile wrote:
>> final size_t select(ref UniformRNG urng)
>> {
>> assert(_recordsRemaining > 0);
>> assert(_sampleRemaining > 0);
>
> Probably it's better to move those asserts in preconditions/postconditions or in
> class/struct invariants.

Those asserts are deliberately intended for the function.  To see why it 
matters, consider setting up an instance of the class to select a sample of size 
5, and then calling select() 6 times.

Of course, there may be a better way of handling that which I'm not aware of :-)

> And I think your code needs some unittests{} too.

Agreed.

>> Ack, enabling that is precisely why I bothered templatizing it in the first
>> place. Can you suggest a fix ... ? I don't understand why as-is it wouldn't work.
>
> If you try to use a Xorshift the compiler will tell you the problems, I think
> with some work you will be able to fix them and create a more flexible module:
>
> void main() {
> auto urng = Xorshift(23);

I realized afterwards what you were talking about, but am not sure how to fix 
(cf. my other email).  My first coding attempt was designed to be more flexible 
but the compiler didn't like it. ;-)

> In my opinion demo code is a bit like unittests. And unittests require the same
> coding precision as the rest of the code :-)

Agree :-)  They are imperfect tests for now, IMO, which is why I'm being lazy...



More information about the Digitalmars-d-learn mailing list