How do I sample randomly based on some probability(wightage)?

Scott David Daniels Scott.Daniels at Acm.Org
Sun May 31 21:15:51 CEST 2009


Peter Otten wrote:
> Scott David Daniels wrote:
>  [a real correction and a bogus one]
>> raise ValueError('Samplng more than %d without replacement?' % (
>> sum(1 for w in weights)))
> That would be len(weights)
Yup, probably I should have just saved len(combined).

> but I think your first version was correct; I don't see were you remove the 
> 0-items from weights.

You are right.  I thought I was looking at combined on the second,
realized my mistake, and thought, Oh well, it'll work out later.
The other mistake wasn't too bad either, as all that would happen
is that an internal exception would get raised, any answers delivered
are delivered with the correct probabilities.

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list