I was trying to do this with a while loop that generates random integers until the next bucket isn't equal to the previously used bucket, but that seems inefficient, and was hoping someone else might have a better algorithm to solve this problem.

2 Answers
2

You could generate a random list of the buckets, and then randomly pick from then in order, removing the bucket from the list when you pick from it. When the list is empty, regenerate a random list of buckets, repeating until you pick the desired number of items.

Can you repeat items from the buckets? So if you pick the 1st "a" from bucket A the first time around, can you pick it a 2nd time? That'll change the solution.

Thanks, I will give your solution a try. And to answer your question, you cannot pick from the same bucket twice so no repeating characters in the final list like: [.., a, a, ...], but another item can be in between like: [.., a, b, a, ...]
–
JohnJun 4 '12 at 17:46

this answer is close, but actually we need to re-insert the bucket to a random place every other time. Say you first pick a item from bucket A, then you remove bucket A, and pick another item from bucket B, and remove bucket B, now you need to insert bucket A back
–
xvatarJun 4 '12 at 18:45

@xvatar Great idea! I've also added checks to make sure the bucket isn't empty before adding the bucket back to the pick list.
–
JohnJun 4 '12 at 21:18

Edited in response to the constraint that no draw must be consecutive from each bucket. It's simple to throw away permutations that don't meet your criteria. Now that this will fail (as is) if two buckets have identical "labels".

Thanks for the answer, but I'm trying to avoid language specific libraries like itertools. I've edited the original question to reflect this requirement.
–
JohnJun 4 '12 at 18:58

@John my fault, for some mistaken reason I thought this was tagged in Python. The idea should work in any language though, generate n/L blocks of the labels - permute them - check if the draw is valid (i.e. no consecutive draws) - if not repeat. All itertools does here is flatten the list, it can be removed.
–
HookedJun 4 '12 at 19:00