[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Weighted sampling



On Tue, Mar 20, 2001 at 10:01:59AM +1200, Jasvir Nagra wrote:
> 
> #!/usr/bin/perl
> 
> @weights = (1,0,2);
> @cweight = map { $sums += $_ } @weights;
> 
> print &sample;
> 
> sub sample {  
>   $rand = rand $cweight[-1];  
>   for (0..$#cweight) {    
>      return $_ if $rand < $cweight[$_];  
>   }
> }


That uses O (n) additional memory.



Abigail

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe