Weighted "random" selection from list of lists
rrr at ronadam.com
Sat Oct 8 20:02:30 CEST 2005
Jesse Noller wrote:
> 60% from list 1 (main_list)
> 30% from list 2 (main_list)
> 10% from list 3 (main_list)
> I know how to pull a random sequence (using random()) from the lists,
> but I'm not sure how to pick it with the desired percentages.
> Any help is appreciated, thanks
Just add up the total of all lists.
total = len(list1)+len(list2)+len(list3)
n1 = .60 * total # number from list 1
n2 = .30 * total # number from list 2
n3 = .10 * total # number from list 3
You'll need to decide how to handle when a list has too few items in it.
More information about the Python-list