Range of random numbers

jerro a at a.com
Mon Apr 23 07:58:00 PDT 2012


> 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
>
> In the meantime cycle([0]) is acceptable but slower (untested):
>
> return cycle([0]).map!(_ => uniform(lower, upper))();

He could also use repeat in this case:

return repeat(0).map!(_ => uniform(lower, upper))();

repeat(0) returns the same sequence as cycle([0]) and is as fast
as it gets, since popFront does nothing and empty is an enum.


More information about the Digitalmars-d-learn mailing list