A Tausworthe random number generator

monarch_dodra monarchdodra at gmail.com
Tue Jan 21 05:44:08 PST 2014


On Tuesday, 21 January 2014 at 11:04:13 UTC, terchestor wrote:
> Well, thanks for the advices.
> Here it is: 
> https://github.com/Terchestor/dlang/tree/Tausworthe-class
>
> Please be indulgent ,-)

The code feels a bit "C-ish", but I'd argue those are just style 
issues.

My major points would be:
1) Make it a "final class": By default, classes have all their 
methods virtual. I don't think you want that.

2) I'd adhere to the "Range" interface: Give your function the 
"front"/"popFront()" functions, as well as the public "enum empty 
= false".

3) I think the *instance* itself should carry the bounds on 
initialization, and provide a single "uniform" function.

4) Uniform initialization: provide a "tausworthe(I = int, R = 
double)(int seed, double low = 0.0, double high = 1.0 )" 
function. This function should simply return a new instance of a 
Tausworthe. The advantage of this are 2-fold:
4.1) Similar syntax for struct/class initialization
4.2) Default Type parameters for the type.

The combination of 2/3/4 will allow you to write nifty things 
like:
int[] myRandomNumbers = tausworthe(randomSeed).take(10).array();

And voila! An array of 10 random numbers! Easy-peasy.


More information about the Digitalmars-d mailing list