passing a variadic parameter to randomSample

forkit forkit at gmail.com
Tue Jan 25 21:55:03 UTC 2022


On Tuesday, 25 January 2022 at 11:50:08 UTC, vit wrote:
>

thanks. problem solved (providing all parameters are of the same 
type).

// ---

module test;
import std;

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

void main()
{
     writeln( RandomChoice("typeA", "typeB", "typeC") );
     writeln( RandomChoice(5, 8, 2) );
     writeln( RandomChoice(100.05, 110.8, 109.54) );

     //writeln( RandomChoice("typeA", 5, 100.14) ); // nope. they 
all need to be of the same type.
     writeln( RandomChoice("typeA", 5.to!string, 100.14.to!string) 
);
}

//--


More information about the Digitalmars-d-learn mailing list