Weighted "random" selection from list of lists
Ron Adam
rrr at ronadam.com
Sat Oct 8 14:02:30 EDT 2005
Jesse Noller wrote:
> 60% from list 1 (main_list[0])
> 30% from list 2 (main_list[1])
> 10% from list 3 (main_list[2])
>
> 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
>
> -jesse
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.
Cheers,
Ron
More information about the Python-list
mailing list