A Tausworthe random number generator
terchestor
terchestor at gmail.com
Tue Jan 21 03:04:12 PST 2014
On Tuesday, 21 January 2014 at 10:10:32 UTC, John Colvin wrote:
> On Tuesday, 21 January 2014 at 10:05:42 UTC, terchestor wrote:
>> I'm a newcomer to D language (after years of programming with
>> ASM Motorola 68030-40 long time ago, as well as C, C++,
>> Javascript, PHP, etc.), but not anymore a professional coder.
>> After reading some articles around there and that nice and
>> cute TDPL (just after Coding standards, another world!), I
>> began to write some to exercise my fresh new "skill".
>> I choose as a first experience, to write a simple class
>> implementing a very interesting random number generator known
>> as Tausworthe, according to the recipe found in this paper:
>> http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.101.9723&rep=rep1&type=pdf.
>> The algorithm in C is quite simple and set in Lecuyer's paper
>> as:
>>
>> unsigned long s1, s2, s3, b;
>> double taus88 ()
>> {
>> /* Generates numbers between 0 and 1. */
>> b = (((s1 << 13) ^ s1) >> 19);
>> s1 = (((s1 & 4294967294) << 12) ^ b);
>> b = (((s2 << 2) ^ s2) >> 25);
>> s2 = (((s2 & 4294967288) << 4) ^ b);
>> b = (((s3 << 3) ^ s3) >> 11);
>> s3 = (((s3 & 4294967280) << 17) ^ b);
>> return ((s1 ^ s2 ^ s3) * 2.3283064365e-10);
>> }
>>
>> I made some "adjustments" to this algorithm, to yield either a
>> (long) integer or (double) float result, in any range.
>>
>> Benchmarks
>> ----------
>> Tausworthe module (rdmd -main -unittest
>> -debug=Benchmark_Tausworthe tausworthe.d)
>> ============================================================
>> Call 1_000_000 times benchmark_uniform_ulong( ): 30 ms |
>> Average : 3e-05 ms
>> ============================================================
>> ============================================================
>> Call 1_000_000 times benchmark_uniform_double( ): 42 ms |
>> Average : 4.2e-05 ms
>> ============================================================
>> ============================================================
>> Call 1_000_000 times benchmark_uniform_std( ): 70 ms | Average
>> : 7e-05 ms
>> ============================================================
>> Comparing ratio tausworthe / std.random: 0.609229
>>
>> I'd greatly appreciate any reviewing and testing of my code.
>> Any improvements you feel necessary are most welcome (as said
>> before, I'm a newbie).
>>
>> What's the best place to publish the code?
>
> Github, bitbucket or equivalent.
>
> If you think it's ready for general use then write a dub.json
> and register it to code.dlang.org
>
>
> Take care with modifying random number generators, it's quite
> easy to ruin them by changing something that seems innocent.
Well, thanks for the advices.
Here it is:
https://github.com/Terchestor/dlang/tree/Tausworthe-class
Please be indulgent ,-)
More information about the Digitalmars-d
mailing list