|  | Posted by Ewoud Dronkert on 05/24/05 18:17 
On Tue, 24 May 2005 14:28:44 +0100, Matt Raines wrote:> Perhaps I didn't make it clear that [...]
 
 No, sorry, my fault. I half expected one thing then didn't read on very
 well.
 
 Your solution is nifty, but rather expensive because of every call to
 mt_rand() or rand() on each line of the file, especially if every word
 chosen requires another complete walk of the file (or concurrent but
 different rand() calls). My algorithm requires only one walk of the file
 for any number of random words picked, or two if the number of lines is
 not known.
 
 What is the best way to pick k numbers from range n while optimizing
 speed, storage and/or randomness? Maybe:
 
 $n = 1000;
 $k = 2;
 $a = range( 0, $n - 1 );
 for ( $i = 0; $i < $k; ++$i )
 {
 $j = mt_random( $i, $n - 1 );
 $a[$i] = $a[$j];
 $a[$j] = $i;
 }
 $b = array_slice( $a, 0, $k );
 
 (Still just as many calls to mt_rand() as no. of words picked).
 Btw, is this for-loop (but then with $i<$n) the way the shuffle function
 is implemented?
 To prep $b for acting as $skip from my first post:
 
 sort( $b );
 for ( $i = 1; $i < $k; ++$i )
 $b[$i] -= $b[$i - 1] + 1;
 
 
 --
 Firefox Web Browser - Rediscover the web - http://getffox.com/
 Thunderbird E-mail and Newsgroups - http://gettbird.com/
 [Back to original message] |