diff --git a/src/classes/batchSegmentCreator.cls b/src/classes/batchSegmentCreator.cls index 271634e..78ace3d 100644 --- a/src/classes/batchSegmentCreator.cls +++ b/src/classes/batchSegmentCreator.cls @@ -33,6 +33,7 @@ gglobal class batchSegmentCreator implements Database.Batchable,Databas global List segmentCampaignIds; global List segmentSizes; global List membersAdded; + global List sizesCDF; global String errorTxt = ''; global boolean keepOriginal; @@ -57,7 +58,18 @@ gglobal class batchSegmentCreator implements Database.Batchable,Databas Boolean assigned = false; while(assigned == false){ - Integer assignedList = Math.floor(segmentCampaignIds.size() * Math.Random()).intValue(); + Integer assignedList; + Double prob = Math.Random(); + + // iterate through sizesCDF to find correct bucket + // note that sizesCDF must be sorted from smallest to largest + // probability (and should be given the generation process above) + for (Integer i=0; i= prob) { + assignedList = i; + break; + } + } if((membersAdded[assignedList] < segmentSizes[assignedList])){ newCampaignMembers.add( @@ -202,4 +214,4 @@ gglobal class batchSegmentCreator implements Database.Batchable,Databas System.AssertEquals(database.countquery('SELECT count()' +' FROM CampaignMember WHERE campaignId=\'' + mySegmentIds[1] + '\''), 100); } -} \ No newline at end of file +} diff --git a/src/classes/memberAssignment.cls b/src/classes/memberAssignment.cls index 66adb32..92c2610 100644 --- a/src/classes/memberAssignment.cls +++ b/src/classes/memberAssignment.cls @@ -34,6 +34,8 @@ public with sharing class memberAssignment { static List members; static List newCampaignMembers; static Boolean keep; + static Integer totalSize; + static List sizesCDF; public static boolean assignMembersNow(Id parentCampaignId, List segmentCampaignIds, List segmentSizes, List membersAdded, Boolean keepOriginal){ campId = parentCampaignId; @@ -44,11 +46,31 @@ public with sharing class memberAssignment { newCampaignMembers = new List(); List oldMembers = [select id,campaignId, leadId, contactid from campaignMember where CampaignId = :campId]; + + // Figure out the fraction that should go into each list, + // then generate a cumulative distribution function to + // use in making the assignments + totalSize = 0; + for (Integer s : sizes) { + totalSize += s; + } + + // Generate the CDF + sizesCDF = new List(); + sizesCDF.add(1.0*sizes[0]/totalSize); + for (Integer i=1; i