mardi 30 octobre 2018

assigning random values from List into List C#

I'm working on a code that assigns a random numbers from List A into a group of objects in List B

Here is the details:

I have a List of Campaigns and List of CampaignRecipients, and I need to cover three scenarios:

  1. When I have equal count on both lists, 2 campaigns and 2 recipients "I have no issue with it at all, it randomly picks a campaignID and assigns it to a randomly picked recipient from List B and so on.
  2. When I have 3 campaigns and 1000 recipients, so it will divide the List of recipients into three groups and assigns each group a randomly picked campaign ID.
  3. When having 5 campaigns and 3 recipients, then it will randomly pick 3 campaigns and assigns them to the recipients.

In point 2 where I'm having a problem with it and it takes very long time as i stated, it distributes the numbers as I want but it is very slow when dealing with 1k recipients or more.

Here is my code, maybe i'm doing something wrong, any suggestion of change needed? Note: i have no problem to change the approach if needed

private static void RandomizeScenarios(ref IList<CampaignLib> cmp, ref IList<CampaignRecipientLib> rec)
        {
            IEnumerable<int> RecipientsIds = rec.Select(x => x.ID).ToList();
            IList<int> CampaignsIds = cmp.Select(x => x.CampaignId.Value).ToList();
            int initVal = RecipientsIds.Count() / CampaignsIds.Count;
            int i = 0;

            if (CampaignsIds.Count < rec.Count())
            {
                List<CampaignRecipientLib> tmpRecipients = new List<CampaignRecipientLib>();

                foreach (var item in CampaignsIds)
                {
                    i++;
                    IEnumerable<int> tmp = null;

                    if (i < CampaignsIds.Count) tmp = RecipientsIds.Shuffle().Take(initVal);
                    else tmp = RecipientsIds.Shuffle().Take(RecipientsIds.Count());


                    RecipientsIds = from r in RecipientsIds where !tmp.Contains(r) select r;

                    var PartialRecipients = from r in rec where tmp.Contains(r.ID) select r;


                    // HERE IT TAKES A VERY LONG TIME < 35mins for 2.5K objects
                    PartialRecipients.ToList().ForEach(r => r.CampaignId = item);

                    tmpRecipients.AddRange(PartialRecipients);

                }
                rec = tmpRecipients;
            }
            else if (CampaignsIds.Count == rec.Count())
            {
                foreach (var item in CampaignsIds)
                {
                    int tmp = RecipientsIds.Shuffle().Take(1).FirstOrDefault();

                    RecipientsIds = from r in RecipientsIds where tmp != r select r;

                    rec.FirstOrDefault(x => x.ID == tmp).CampaignId = item;                  
                }
            }
            else if (CampaignsIds.Count > rec.Count())
            {
                foreach (var item in CampaignsIds.PickRandom(RecipientsIds.Count()).OrderBy(x => x))
                {
                    int tmp = RecipientsIds.Shuffle().PickRandom(1).FirstOrDefault();

                    RecipientsIds = from r in RecipientsIds where tmp != r select r;

                    rec.FirstOrDefault(x => x.ID.Equals(tmp)).CampaignId = item;
                }
            }

        }




Aucun commentaire:

Enregistrer un commentaire