random k-sample of a file

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Thu Oct 9 16:01:18 PDT 2008


Russell Lewis wrote:
> BEGIN CODE
> char[][] choose(int k)
> {
>   char[][] bufs; bufs.length = k;
> 
>   for(int i=0; i<k; i++)
>     bufs[i] = ReadALine();
> 
>   while(!feof(stdin))
>   {
>     int i = random(0,k);
>     bufs[i] = ReadALine();
>   }
> 
>   return bufs;
> }
> 
> END CODE

Then the last line will be part of the selection with probability 1.

Andrei



More information about the Digitalmars-d mailing list