Experimental approach to reference-type random number generators
bearophile
bearophileHUGS at lycos.com
Sun Sep 1 15:54:54 PDT 2013
Joseph Rushton Wakeling:
> auto uniDist = uniformDistribution(0.0, 1.0);
> auto biasedDice = diceDistribution(10, 5, 7);
> foreach(u, d; lockstep(uniDist, biasedDice).take(10))
> {
> writeln(u);
> writeln(d);
> }
>
> (I'm sure you can find something to improve in how I've written
> the second example, but you get the idea of how the
> distributions could behave:-)
Perhaps it's better for it to only accept a range (despite this
causes a memory allocation):
diceDistribution([10, 5, 7])
It's usable like this:
[0.3, 0.5, 0.1, 0.1].diceDistribution.map!(i =>
"ACGT"[i]).take(10).writeln;
diceDistribution() is much more efficient than calling dice()
many times in map.
In the enhancement request I have also suggested to support an
optional list of items, so the code becomes:
[0.3, 0.5, 0.1, 0.1].diceDistribution("ACGT").take(10).writeln;
The name "diceDistribution" is rather long, but at the moment I
don't know of better names. "diceDist" is shorter, but less clear.
I usually don't have a continuous distribution to sample (beside
the most common ones, like the gaussian), so for me a
diceDistribution is enough for now.
Bye,
bearophile
More information about the Digitalmars-d
mailing list