[Solved] How do I check multiple probabilities?

0 favourites
From the Asset Store
Be quick and choose the right answer for the shown equation.
  • Hello.

    I use an 2D array to store probabilities.

    Lets say the first Y row represents the chances a character has to die from several death causes.

    X1 is the chance to die from hearth attacks. X2 is the chance to starve. X3 the chance from being hit by an asteroid.

    I now have to check which of the probabilities do occur... or maybe non of them occur.

    How can I now check by which cause the character dies (if he even dies)? I need to put these probabilities in If conditions depending on each other.

    Lets assume X1=35% , X2=60%, X3=80%.

    Do I have to sum each probabilities?

    100%-35%=65%

    100%-60%=40%

    100%-80%=20%

    175% / 125% chance to die.

    Thanks in advance!

    I hope you know what I mean. It is hard to describe.

  • I know what you mean but this is maths, not C2 related.

  • I recently had the same question, check it out:

    There's a lot of math involved with making games. If you don't want to help, then don't reply

  • Actually.. I'd go as far as saying that the most of the how-do I questions seem to be related to math one way or another. Distances, speeds, enemy prediction, probabilities, Logic (or lack of), trigonometry, basic matrix math, 3d/isometric projections. Makes me wonder don't they teach math anymore?

    I need to think.. There was a time I knew things like this. it's been so many years I have forgotten..

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thank you all for your replies!

    I have an idea but i don't know if I'm right:

    if random <= X1 - chane 1 occurs

    if random > X1 and <= X1+X2 - chance 2 occurs

    if random > X1+X2 and <= X1+X2+X3 - chance 3 occurs

    if random > X1+X2+X3 - nothing occurs

    Is this right? Does this keep the right % chances?

  • Worth reading through the thread that 7Soul posted ...

  • Okay I will read through it. I have not had the time for it yet.

    Edit:

    Okay I read through the topic. I already had this solution in mind but my case is a little bit different so it whould not work for me (I think).

    I safe my probabilities in an array and each of this probabilities can be ranging from 0%-100%. So if there are for example 5 different probabilities their sum can be greater or smaller than 100%.

    I will show you an example. I attached a picture of the array. Each of this values can range from 0-100, representing % chances. 50% is the base value at the start of the game. The values are dynamic. Each Y row is an action the AI can make and each X row is the % chance of the object he is using for this action.

    Let's say the first action (Y 0) is "destroy". And let's say the AI has 6 things he is able to destroy. X1=window, X2=door, X3=car, X4=house, X5=smartphone, X6=TV.

    First i randomly choose an Y value to determine which action the AI is doing. After that I have to randomly let the AI pick an X value to determine with which object it is interacting. And this should be dependent on the % chances. If X1 is 0% the AI whould never destroy a window if he wants to destroy something. If X3 and X5 are both 100% than it whould always destroy either a car or a smartphone if the AI decides to destroy something.

    So i have to set those values in relation to each other, but have to keep their own % chances. My had is about to explode.

    I hope my problem is now clearer. There might be an easier solution than my approach but It gives me hadache.^^

    Thanks for reading

  • Hi.. After some consideration of your original question. And assuming these negative events are independent on one another.

    I'd say that the way to see survival likelihood is to multiply the survival rates together.

    So the odds of survival = .65 * .40 * .20 = 0.052 .. is slightly over 5% ...

  • The way I do this is

    step through each probability from high to low ( the high to low bit is important)

    on each step I set the return value appropriately.

    lets take your first example -

    80% 60% and 30%

    so you would step through these in high to low order

    and lets say our percentage to check is 50%

    so it would pass the first two (< 80 and < 60 ) but fail the third.

    If you have 3 or 10 possibilities it makes no real difference (providing they are all different)

    In your latest version you would need to take your (5 ?) possible % and pass them (hight to low) to a function that just does the above. This function would be probably be easier to write if you had a fixed number of possibilities (5,8,10, whatever) even if some of these possibilities were just padding.

  • Thank you Joannak for your afford. But I described my problem badly in the first post.

    And RamPackWobble whank you, too. But what if the first probability is 100%? Than the others whould never occur.

    I have to find another way around this. :/

    Edit:

    And what if I instead go from low to high? So it will pass all 0% chances (they whould anyways never occur). But if the last 2 are 100% there is the same problem.

    I think I now have a solution:

    roll=round(random(X1+X2+X3+X4+X5+X6...))

    if roll <=X1 - case 1

    if roll >X1 & roll <= X1+X2 - case 2 occurs

    if roll >X1+X2 & roll <= X1+X2+X3 - case 3 occurs

    if roll >X1+X2+X3 & roll <= X1+X2+X3+X4 - case 4 occurs

    if roll X1+X2+X3+X4 & roll <= X1+X2+X3+X4+X5 - case 5 occurs

    if roll X1+X2+X3+X4+X5 & roll <= X1+X2+X3+X4+X5+X6 - case 6 occurs

    So this whould ensure, that the AI does check all probabilities, even if there are more at 0% or 100%. The other good thing about this whould be that there is always an X case choosen (except all cases have 0% chance).

    The values itself are dynamic but the number of X cases are static. So I think I could use this. Whould this be correct on the maths side?

  • by going hi to low with

    100%(returning 1)

    90 %(returning 2)

    80%(returning 3)

    70%(returning 4)

    60%(returning 5)

    as probabilities with the "found" being 75%

    then first time 100 would set the return to be "1" but this would then be over written by the 90 setting the return to be "2" which would then be overwritten by the 80 setting the return to be "3" the 70 would not alter the return value nor would the 60 so the return ("3") would indicate it fell into the 80% range...

  • But if 2 or more values are the same than the first ones whould never occur:

    100%(returning 1, but will never occur)

    100%(will always overwrite 1 to 2)

    80%(returning 3)

    70%(returning 4, but will never occur)

    70%(will always overwrite 4 to 5)

    I will try out if I can use my solution and if the % chances keep correct. I will loop through the code 10,000 times like you did in the other thread.

  • True - so you could also include a check for that and then pick one at random ? But, what is the chance of there being 2 or more with the same chance and would it matter if it picks the lower one in the list ?

    This is the method I've used in the past and it worked for me - maybe you can take the idea and alter it to your needs or maybe you need another approach.

  • Yes it needs to be possible for each case to occur. Because in my example the AI whould never destroy the window if he wants to destroy something.. even though he has a 100% chance to want to destroy a window.

    I'm currently setting up the 10,000 loop and I then will post the .capx.

  • I'm stuck in the complicated percentage calculation.

    It's actually not "per cent" (/100) it's "per total amount of all values of the X array row)

    I am trying to figure out how I have to set up the equation.

    Rolls is the total number of rolls and X0 is the total amount of the occurring X0 cases. I want to figure out at how many % X0 occurs:

    Set Text:

    round(rolls/x0*(array.At(0,0)+array.At(1,0)+array.At(2,0)+array.At(3,0)+array.At(4,0)+array.At(5,0)+array.At(6,0)+array.At(7,0)+array.At(8,0)+array.At(9,0))) & "% dynamic chance"

    Edit:

    Awesome! I figured it out. I had to swap "rolls/x0" to "x0/rolls". I will soon upload the .capx so everyone who will need this can look at it.^^

    Edit 2:

    Here is the .capx. On the top left you can see all rolls. On the left the static % chances given in the array. On the right side the dynamic case occurences.

    Edit 3:

    I updated the .capx so everyone, who needs this solution, can easily comprehend how it works.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)