Reputation: 23098
Given a list of photos:
set 1 : 14 photos
set 2 : 4 photos
set 3 : 2 photos
[{'set' : 1,'photos' : ['photo1','photo2'....'photo14']}, {'set' : 2,.....]
I want to display 10 images to the user. I want it so that set 1 is most likely to dominate the list (say, 6 out of 10 images) whereas the other sets are not unfairly penalized for having small numbers.
What is a simple robust algorithm which results in such esthetically pleasing random choices?
Upvotes: 0
Views: 73
Reputation: 213015
import random
photos = [{'set' : 1,'photos' : ['photo1','photo2'....'photo14']}, {'set' : 2,.....]
weight = {1: 6, 2: 3, 3: 1} # set : number of shown photos
show = []
for d in photos:
show.extend(random.sample(d['photos'], weight[d['set']]))
random.shuffle(show)
# show now contains a shuffled list of 6 photos from set 1, 3 from set 2 and 1 from set 3
Upvotes: 4