random.sample with large weighted sample-sets?
Terry Reedy
tjreedy at udel.edu
Sun Feb 16 04:12:22 EST 2014
On 2/15/2014 11:41 PM, Tim Chase wrote:
> I'm not coming up with the right keywords to find what I'm hunting.
> I'd like to randomly sample a modestly compact list with weighted
> distributions, so I might have
>
> data = (
> ("apple", 20),
> ("orange", 50),
> ("grape", 30),
> )
If you actually start with date in this form, write the few lines needed
to produce the form below.
import bisect
import random
data = [
(0, 'apple'),
(20, 'orange'),
(70, 'grape'),
]
for i in range(10):
r = random.randrange(0, 100)
i = bisect.bisect(data, (r, 'zzzzz')) - 1
print(data[i][1])
>>>
apple
orange
orange
grape
orange
apple
grape
orange
grape
orange
It is just coincidence that the sample has exactly the expected
distribution.
--
Terry Jan Reedy
More information about the Python-list
mailing list