0

I am after some help trying to get my head around the logic needed to generate a random number based on 3 different probabilities.

My example is this: I have an array of objects. Upon each cycle, this array can change in size (some objects added, some removed). Each object has a property called Priority which is set to either High, Medium or Low.

I need an algorithm to pick a random object from the array but based on the Priority. So objects with a High priority will have a higher probability of getting picked than objects with a Low priority.

I took this approach, manually creating an IndexArray and adding the index multiple times based on the priority each cycle and then picking a random index. So, for example:

switch( campaign.Priority ) {
    

    // For high priority campaigns, probability is 6 out of 10
    case 'High':
        for( let i = 0; i < 6; i++ ) {
    
            randomCampaignArray.push( idx );       

        }
        break;   


    // For medium priority campaigns, probability is 3 out of 10
    case 'Medium':
        for( let i = 0; i < 3; i++ ) {     

            randomCampaignArray.push( idx );
        
        }
        break;   


    // For low priority campaigns, probability is 1 out of 10
    case 'Low':
        for( let i = 0; i < 1; i++ ) {        

            randomCampaignArray.push( idx );
        
        }
        break;    


}
    

// Determine a random index from the campaign array
let index = Math.floor( Math.random() * randomCampaignArray.length );
    

// Get a handle to that indexed campaign
_campaign = campaigns[randomCampaignArray[index]];

But then I am left with an Array that looks like [1,1,1,1,1,1,2,2,2,3,3,3,4] which then requires the random value to always be in the low range.

Is there a better and more practical approach to this?

Riples
  • 1,167
  • 2
  • 21
  • 54
  • That didn't answer my question at all...totally different scenario. – Riples Dec 17 '21 at 01:27
  • What's wrong with your approach? I don't see any problem. – Ricky Mo Dec 17 '21 at 01:34
  • It just seems that in the above scenario, objects with a higher probability require the random number to always generate lower. Doesn't seem random to me.... – Riples Dec 17 '21 at 01:36
  • It's just your misconception. The chance of generating a number in 0 - 0.5 is equal to the chance of generating a number in 0.5 - 1.0. As long as the length of the ranges are equal, it doesn't matter it locates in the low side or the high side, chances are equal. – Ricky Mo Dec 17 '21 at 01:44
  • And every random number generation are independent events. Every time a number generated has equal chances of hitting the low side and hitting the high side. It doesn't matter how your elements are distributed in the array, every elements have equal chances to get hit. – Ricky Mo Dec 17 '21 at 01:48
  • Okay, thanks @RickyMo. I appreciate your time and explanation. Maybe it is my misconception. I will use my approach and monitor the results. Thanks – Riples Dec 17 '21 at 01:59

0 Answers0