On 1/25/22 13:55, forkit wrote:

> auto RandomChoice(R...)(R r)

Watch out though: The compiler will compile a different function per set of values. For example, there will be separate RandomChoice instances for ("hello") vs. ("world").

D has a simple variadic parameter syntax as well:

auto RandomChoice(R)(R[] r...)

> {
>      auto rnd = MinstdRand0(unpredictableSeed);
>      return only(r).randomSample(1, rnd).front;

Which makes that simpler as well because being a slice, r is already a range. And there is choice():

    return r.choice(rnd);

Something is very important though: The 'r' slice is short-lived; it does not live in dynamic memory. RandomChoice() should not save it for later use nor return it. (The compiler may have protection against that; I am not sure.)

Ali

Reply via email to