CORRECTION: random k-sample of a file

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


Russell Lewis wrote:
> Sorry, I overlooked the (obvious) fact that this biases things towards 
> the end of the file.  Change the while loop as follows:
> 
> BEGIN CODE
>   int count = k;
>   while(!feof(stdin))
>   {
>     if(random(0,count) >= k)
>       ReadALine();   // this discards the line
>     else
>     {
>       int i = random(0,k);
>       bufs[i] = ReadALine();
>     }
>     count++;
>   }
> END CODE

Yah that works. Nice!

Andrei



More information about the Digitalmars-d mailing list