ForceDotComLabs/Salesforce-Campaign-Segmentation-Wizard — pull request #6 by shaycrk (2014-02-21 22:19:53+00:00)
Title: Assignment probabilities based on segment sizes

We've been using this tool to create segments for email campaigns, but have run into an issue when using it to generate segments of much different sizes.

The current code is looping over parent campaign members in the order they come out of the underlying datastore and attempting to assign to each of the groups with equal probability. In practice, this means small groups fill up first (presumably with older contacts who have lower primary keys) and larger groups end up over-representing contacts who are assigned later (presumably newer contacts with larger primary keys). Additionally, this has a performance impact by continuing to attempt to assign members to the small segments even after they've filled up, resulting in many recursive calls to `assignMember()`.

This patch assigns a probability to each segment based on its relative size and then makes assignments based on those probabilities. As a result, members are added to smaller segments at a slower rate than to the larger segments, providing a more even distribution of assignments relative to the initial ordering of the contacts and fewer recursive calls.