Range of random numbers

Dmitry Olshansky dmitry.olsh at gmail.com
Mon Apr 23 07:57:19 PDT 2012


On 23.04.2012 17:52, bearophile wrote:
> Joseph Rushton Wakeling:
>
>> struct UniformRange(T1, T2)
>> {
>> T1 _lower;
>> T2 _upper;
>>
>> @property enum bool empty = false;
>>
>> this(T1 a, T2 b)
>> {
>> _lower = a;
>> _upper = b;
>> }
>>
>> @property auto ref front()
>> {
>> assert(!empty);
>> return uniform(_lower, _upper);
>> }
>>
>> void popFront()
>> {
>> }
>> }
>
> What about (untested):
>
> auto uniformRange(T1 lower, T2 upper) {
> return count().map!(_ => uniform(lower, upper))();
> }
>
> Where count() is just:
> http://d.puremagic.com/issues/show_bug.cgi?id=7839
>

What's wrong with:
http://dlang.org/phobos/std_algorithm.html#count

> In the meantime cycle([0]) is acceptable but slower (untested):
>
> return cycle([0]).map!(_ => uniform(lower, upper))();
>
> Bye,
> bearophile


-- 
Dmitry Olshansky


More information about the Digitalmars-d-learn mailing list