Re: passing a variadic parameter to randomSample

2022-01-26 Thread Ali Çehreli via Digitalmars-d-learn
On 1/26/22 07:44, Ali Çehreli wrote: > the instantiations could be too many for R... I am still wrong there. It is inconceivable to instantiate the following template for the same type (e.g. string) from "too many" places in a program: auto RandomChoice(R...)(R r) { // ... } There may be

Re: passing a variadic parameter to randomSample

2022-01-26 Thread Ali Çehreli via Digitalmars-d-learn
On 1/26/22 02:20, Stanislav Blinov wrote: > On Tuesday, 25 January 2022 at 22:07:43 UTC, Ali Çehreli wrote: >> 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 b

Re: passing a variadic parameter to randomSample

2022-01-26 Thread Stanislav Blinov via Digitalmars-d-learn
On Tuesday, 25 January 2022 at 22:07:43 UTC, Ali Çehreli wrote: 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. ("wor

Re: passing a variadic parameter to randomSample

2022-01-25 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Jan 25, 2022 at 10:48:26PM +, forkit via Digitalmars-d-learn wrote: [...] > ... but my main focus here, was learning about variadic template > functions. D has several flavors of variadics: 1) C-style variadics (not type-safe, not recommended): int func(int firstArgc, ...) 2

Re: passing a variadic parameter to randomSample

2022-01-25 Thread forkit via Digitalmars-d-learn
On Tuesday, 25 January 2022 at 22:35:29 UTC, forkit wrote: I should point out (to anyone looking at that code I posted), that it's easier, and makes more sense, to just write: writeln( ["typeA", "typeB", "typeC"].choice ); ... but my main focus here, was learning about variadic template f

Re: passing a variadic parameter to randomSample

2022-01-25 Thread forkit via Digitalmars-d-learn
On Tuesday, 25 January 2022 at 22:07:43 UTC, Ali Çehreli wrote: thanks. makes it even shorter and simpler :-) // -- module test; @safe: import std; auto RandomChoice(R...)(R r) { auto rnd = MinstdRand0(unpredictableSeed); return only(r).choice(rnd); } void main() { writeln( Ra

Re: passing a variadic parameter to randomSample

2022-01-25 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Jan 25, 2022 at 02:07:43PM -0800, Ali Çehreli via Digitalmars-d-learn wrote: [...] > auto RandomChoice(R)(R[] r...) > > > { > > auto rnd = MinstdRand0(unpredictableSeed); > > return only(r).randomSample(1, rnd).front; > > Which makes that simpler

Re: passing a variadic parameter to randomSample

2022-01-25 Thread Ali Çehreli via Digitalmars-d-learn
r 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); So

Re: passing a variadic parameter to randomSample

2022-01-25 Thread forkit via Digitalmars-d-learn
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

Re: passing a variadic parameter to randomSample

2022-01-25 Thread WebFreak001 via Digitalmars-d-learn
On Tuesday, 25 January 2022 at 09:48:25 UTC, forkit wrote: so I'm trying to write (or rather learn how to write) a 'variadic template function', that returns just one of its variadic parameter, randomly chosen. But can't get my head around the problem here :-( .. Error: template `std.random.r

Re: passing a variadic parameter to randomSample

2022-01-25 Thread WebFreak001 via Digitalmars-d-learn
On Tuesday, 25 January 2022 at 09:48:25 UTC, forkit wrote: so I'm trying to write (or rather learn how to write) a 'variadic template function', that returns just one of its variadic parameter, randomly chosen. But can't get my head around the problem here :-( .. Error: template `std.random.r

Re: passing a variadic parameter to randomSample

2022-01-25 Thread vit via Digitalmars-d-learn
quot;, "typeC") ); } // -- `r` is not input range, try this: ```d module test; import std; string RandomChoice1(R...)(R r) { auto rnd = MinstdRand0(unpredictableSeed); return only(r).randomSample(1, rnd).front; } string RandomChoice2(R...)(R r)@nogc { auto rnd = Min

passing a variadic parameter to randomSample

2022-01-25 Thread forkit via Digitalmars-d-learn
so I'm trying to write (or rather learn how to write) a 'variadic template function', that returns just one of its variadic parameter, randomly chosen. But can't get my head around the problem here :-( .. Error: template `std.random.randomSample` cannot deduce function from argument types `

Re: Working with randomSample

2018-12-28 Thread Murilo via Digitalmars-d-learn
On Friday, 28 December 2018 at 03:59:52 UTC, Ali Çehreli wrote: It's because randomSample returns either an input range or a forward range depending both on the kind of range that it gets and the random number generator used. Documented here: Ali Thank you very much, now it all makes sense.

Re: Working with randomSample

2018-12-27 Thread Ali Çehreli via Digitalmars-d-learn
On 12/27/2018 06:06 PM, Murilo wrote: > Why is it that when I type "auto choice = randomSample(array);" and > later when I try to index choice as in choice[1] it gives an error message? It's because randomSample returns either an input range or a forward range dependin

Working with randomSample

2018-12-27 Thread Murilo via Digitalmars-d-learn
Why is it that when I type "auto choice = randomSample(array);" and later when I try to index choice as in choice[1] it gives an error message?

Re: randomSample

2014-05-18 Thread bearophile via Digitalmars-d-learn
Meta: You need to use the function array from std.array. import std.array; int[] source = [ ... ]; int[] sample = randomSample(source, 3).array(); In some cases it's also useful to use std.algorithm.copy: void main() { import std.stdio, std.algorithm, std.random, std.

Re: randomSample

2014-05-17 Thread Meta via Digitalmars-d-learn
On Sunday, 18 May 2014 at 04:19:05 UTC, David Held wrote: How do I get an array from randomSample()? int[] source = [ ... ]; int[] sample = randomSample(source, 3); src\main.d(30): Error: cannot implicitly convert expression (randomSample(source, 3u)) of type RandomSample!(int[], void) to

Re: randomSample

2014-05-17 Thread David Held via Digitalmars-d-learn
On 5/17/2014 9:18 PM, David Held wrote: How do I get an array from randomSample()? int[] source = [ ... ]; int[] sample = randomSample(source, 3); src\main.d(30): Error: cannot implicitly convert expression (randomSample(source, 3u)) of type RandomSample!(int[], void) to int[] [...] Even

randomSample

2014-05-17 Thread David Held via Digitalmars-d-learn
How do I get an array from randomSample()? int[] source = [ ... ]; int[] sample = randomSample(source, 3); src\main.d(30): Error: cannot implicitly convert expression (randomSample(source, 3u)) of type RandomSample!(int[], void) to int[] I get that RandomSample is a struct which implements

Re: Trying to understand RandomSample struct in std.random

2012-04-12 Thread Ali Çehreli
On 04/12/2012 01:27 PM, Joseph Rushton Wakeling wrote: > What I _still_ don't understand is the statement in the constructor: > > // we should skip some elements initially so we don't always > // start with the first > > ... as it seems to me that doing so would bugger up the selection > algorith

Re: Trying to understand RandomSample struct in std.random

2012-04-12 Thread Joseph Rushton Wakeling
On 12/04/12 20:39, Ali Çehreli wrote: That's misleading. RandomSample is a lazy range. The output seems to be elements of an array only as you pull data out of this range. Ahhh, it's lazy evaluation. That would explain why you can set ret.gen = gen in the randomSample functions a

Re: Trying to understand RandomSample struct in std.random

2012-04-12 Thread Ali Çehreli
On 04/12/2012 11:30 AM, Joseph Rushton Wakeling wrote: > What gets output at the end is clearly an array containing a subset of > the original input. That's misleading. RandomSample is a lazy range. The output seems to be elements of an array only as you pull data out of this ran

Trying to understand RandomSample struct in std.random

2012-04-12 Thread Joseph Rushton Wakeling
Hello all, I'm trying to understand the internal operations of the RandomSample struct in std.random. What gets output at the end is clearly an array containing a subset of the original input. But I can't understand how this is constructed. The constructor sets various initial