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

Re: [FWP] Weighted sampling



>>>>> "abigail" == abigail  <abigail@foad.org> writes:

abigail> That wouldn't work if @weights contains non-integers, or when the
abigail> sum of the weights is huge.

abigail>     sub sample {
abigail>         my ($count, $sample);
abigail>         for (my $i  = 0; $i < @weights; $i ++) {
abigail>             $count += $weights [$i];
abigail>             $sample = $i if rand $count < $weights [$i];
abigail>         }
abigail>         $sample;
abigail>     }

For a fuller example with that, check out one of my first
WT columns at http://www.stonehenge.com/merlyn/WebTechniques/col09.html

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

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