Calculate Chance

I am in need of a math-magician for this little bit here :smiley:

In this little game, the player presses a button each turn, and will get an item, from 1 of 5 tiers of items. Each turn the player can upgrade their chance by 10% to get an item from a better tier (up to 100% chance increase.

There are a total of 30 items, 6 in each tier.

this is the percent chance increase and how it would look:

Increase – % Change

10% ---------tier 1 = 90, tier 2 = 10, tier 3 = 00, tier 4 = 00, tier 5 = 00

20% ---------tier 1 = 80, tier 2 = 20, tier 3 = 00, tier 4 = 00, tier 5 = 00

30% ---------tier 1 = 70, tier 2 = 20, tier 3 = 10, tier 4 = 00, tier 5 = 00

40% ---------tier 1 = 60, tier 2 = 20, tier 3 = 20, tier 4 = 00, tier 5 = 00

50% ---------tier 1 = 50, tier 2 = 20, tier 3 = 20, tier 4 = 10, tier 5 = 00

60% ---------tier 1 = 40, tier 2 = 20, tier 3 = 20, tier 4 = 20, tier 5 = 00

70% ---------tier 1 = 30, tier 2 = 20, tier 3 = 20, tier 4 = 20, tier 5 = 10

80% ---------tier 1 = 20, tier 2 = 20, tier 3 = 20, tier 4 = 20, tier 5 = 20

90% ---------tier 1 = 10, tier 2 = 10, tier 3 = 30, tier 4 = 30, tier 5 = 20

100% --------tier 1 = 0, tier 2 = 10, tier 3 = 30, tier 4 = 30, tier 5 = 30

so… can anyone help me get started on how I would code the probability part of this.

BTW, this is not like a deck of cards, in the sense that chances change based on how many cards are left in the deck. There is an infinite amount of each of the items in all 5 tiers. Only the chance to get a particular item in a tier changes.

Thanks for the help you genuises!!!

To make this nice an expandable and reduce the amount of code, I’ll assume you have your tiers in a nice 2-dimensional array with probability (% change / 100):

var tiers:float[] = [[0.9, 0.1, 0.0, 0.0, 0.0],
                     [0.8, 0.2, 0.0, 0.0, 0.0],
                     ...
                     [0.0, 0.1, 0.3, 0.3, 0.3]];

In this case your luck variable will be a integer value from 0 to 9 (the first column). At any point, if you want to select a random tier (0 to 4 from the tiers), you can do something like this:

function GetRandomTier() {
    //the random value, range = 0, 1
    var r:float = Random.value;

    //previous chance
    var prev:float = 0.0;
    //Iterate through possible tiers
    for (var tier:int = 0; tier < tiers[0].length, tier += 1) {
        //ignore tiers with no chance
        if (tiers[luck, tier] == 0.0) {
            continue;
        }
        
        //check whether chance has given us this tier
        else if (prev <= r && r <= prev + tiers[luck, tier]) {
            return tier;
        }
        
        //update chance range
        prev += tiers[luck, tier];
    }
    
    return 0;
}

This will basically check whether a random value is between each of the ranges of the tiers, assuming all their chances add up to 1, ie. if the tiers are 0.3, 0.6 and 0.1, the ranges checked would be: (0.0, 0.3), (0.3, 0.9) and (0.9, 1.0). There might be some rounding errors or edge cases where this might not work which could be solved by using integers instead of floats, but you should be able to work that out yourself if you have any problems :wink:

Haven’t tested it, but this is a nice way to do what I think you want.