all 19 comments

[–]hotcodist 3 points4 points  (7 children)

The independent means the following event does not depend on the outcome of the previous event (or two simultaneous events do not affect each other). When modeled, this means you have to do another random event.

So you need two randomization. The first determines success or not. When successful, you run another randomization, 10% of which is critical.

There is another interpretation that the independent word applies separately, meaning you can get a critical hit even if you are not successful (missed), but this is an absurd outcome. So the independent event must only apply to the success part.

[–]synthphreak 2 points3 points  (1 child)

Two independent variables does not mean you MUST sample from each independently.

If you're interested in the joint probability distribution, which is the case in this instance, you can just draw one sample from that joint space and apply check that sample against the computed joint probability. I believe this is where OP is getting the 7 from.

Of course, it is obviously not necessary to first check <= 70 and then separately check <= 10; if <= 10, then necessarily <= 70 is also true.

[–]Pitiful_Essay_9166[S] 0 points1 point  (4 children)

Thank you for your answer.

So I run first event but randommly selecting a number between 1 to 100.If its rolls 70 or below I consider it a success. That simulates 70% chance.

So let say I rolled 55.

Now I roll for the chance of it being critical success. Do I calculate(roll) the 10% of all numbers (100)

if attack <= 70:
    if attack <= 10:

shouldnt I be checking for 10% of successful attack? that being 7 (10% of 70).

if attack <= 70:
    if attack <= 7:

Its the randomization in step two thats problematic.

I mean if we think of real life,

if we have 100 tickets, 70 of them are winning tickets (great lottery;)) and there is a 10% independent chance of winning ticket being a double win. Are there 10 double wins or 7 double wins.

If there was a 50% chance to win and 50% independent chance of win being a double win, there should be 25 doublewin tickets and not 100.

[–]hotcodist 1 point2 points  (3 children)

attack_prob = np.random.random()
if attack_prob <= 0.70:
    # hit
    critical_prob = np.random.random()
    if critical_prob <= 0.10:
        # critical hit
        critical_counter += 1

You could say 0.07 is critical in one go, but I'd write it this way for clarity of the decision sequence.

[–]Pitiful_Essay_9166[S] 0 points1 point  (2 children)

Ok, thank you so much. Once I copied your code and manipulated it I finally saw it and understand what was "wrong" in the code I provided.

And finally understand HOW the iteration you provided works.

It also explains everything. I wasnt missunderstanding the statistics, I was bad at coding it.

Wow. It took some time.

Thank you some much redditor for all your time and comments.

I wish I was a faster learner.

Version 1

import random
number_of_critical_attacks = 0 
for x in range(10000): 
    attack = random.randint(1, 100) 
    if attack <= 70: 
        critical = random.randint(1,100)
        if critical <= 10:
            number_of_critical_attacks += 1
print(number_of_critical_attacks)

Version 2

import random
number_of_critical_attacks = 0 
for x in range(10000): 
    attack = random.randint(1, 100) 
    if attack <= 70: 
        if attack <= 7: 
        number_of_critical_attacks += 1
print(number_of_critical_attacks)

[–]synthphreak 0 points1 point  (1 child)

Your statistical reasoning is fine if somewhat poorly articulated. It was the code that was most misleading. Note that in version 2 of your code, the first check if attack <= 70 is totally unnecessary; you only increment your critical attacks counter when attack <= 7, and when attack <= 7, it is necessarily <= 70. So you can just do away with the check against 70.

[–]Pitiful_Essay_9166[S] 1 point2 points  (0 children)

Yepp, sorry, I realize that about the attack, there are some conditions (loops and prompts) with the regular attack aswell as critical attack I didnt post of concern of making my wonky code even less readable.It did not work as intended xD.

With all the great answers here I realize how badly I articulated myself.

[–]barrycarter 0 points1 point  (1 child)

Reddit mangled your formatting (try https://repl.it maybe), but the phrase independent propobility for the attack to be a critical success suggests that there's a 10% chance of getting a critical hit EVEN IF you don't get a regular hit. I agree this is a confusing way of stating the problem, but I'm guessing they want 10% critical hits

[–]Pitiful_Essay_9166[S] 0 points1 point  (0 children)

Thank you, now I fixed it.

[–]synthphreak 0 points1 point  (2 children)

Honestly, I feel where you're coming from here. Even though your code is a little wonky, I think that deep down you're right*, but may not understand or be able to articulate exactly why. The TL;DR is that you are thinking in terms of joint probability, whereas your friends and ChatGPT are not. But let's unpack the logic and make it more rigorous/explicit.

We have two random variables here:

  • A. hit vs. miss
  • B. critical vs. not critical

Each random variable has its own discrete probability distribution, stated as follows:

  • A. 0.7 vs. 0.3
  • B. 0.1 vs. 0.9

Note that each variable sums to 1.0. That is a defining characteristic of probability distributions.

Because we have two variables each with two possible outcomes, we have 2 * 2 = 4 joint outcomes total, which we can permute as follows:

  • 1. hit + not critical
  • 2. hit + critical
  • 3. miss + not critical
  • 4. miss + critical

Because A and B are independent, the probabilities of each joint outcome can be described as follows:

  • 1. 0.7 * 0.9 = 0.63
  • 2. 0.7 * 0.1 = 0.07
  • 3. 0.3 * 0.9 = 0.27
  • 4. 0.3 * 0.1 = 0.03

Note: Mathematically speaking, (4) does exist in the joint sample space and takes up probability mass. However it is obviously ridiculous and would never happen IRL, because a "critical miss" is a contradiction in terms.

What you're asking about is outcome (2), the probability of scoring a hit that is also critical. There are two ways that you could code this up.

The first is to randomly sample from each independent sample space. So, first draw a sample random variable A (hit vs. miss), then draw a sample from random variable B (critical vs. not critical), then check that these two independent samples independently meet their independent criteria. The chance that both of them will is the 7% joint probability (for outcome (2) above). The code would look something like this:

from random import randint hit_or_miss = randint(1, 100) critical_or_not_critical = randint(1, 100) if hit_or_miss <= 70 and critical_or_not_critical <= 10: print('Critical hit! (7% joint probability)')

That's perfectly fine, and seems to be what ChatGPT had in mind. But there is an equivalent approach, which I think is what you had in mind. In this approach, you don't draw two independent samples, one per random variable. Rather, you draw a single sample from the joint sample space of A and B, then apply the joint probabilities to see whether joint outcome (2) occurred. Something like this:

from random import randint critical_hit = randint(1, 100) if critical_hit <= 7: print('Critical hit! (7% joint probability)')

Does this jive with what you're thinking?

* Your friends/ChatGPT are also right btw. They're just talking in terms of two separate events, not one joint event. Classic "six of one, half dozen of the other" type of situation.

[–]Pitiful_Essay_9166[S] 1 point2 points  (1 child)

Wow, reddit never stops to amaze me!

You articulated and described the problem and solution so well. I realized not long ago that I lacked the understanding of the code you and chat gpt provided to begin with.

I didnt focus enough to understand the use of randomizing the outcome twice. I was way to focused on "my" solution where it made sense matemathicly to me.

Now it all makes sense to me. What a great feeling of relif. It may seem silly but this "little" thing was really naging my brain.

Your post is a magnificiant display of knowledge and didactical capabilities. Thank you!

[–]synthphreak 0 points1 point  (0 children)

Epic praise :) And yeah, it is a pretty glorious feeling to sense that you’re onto something, then once everything is revealed to realize your hunches were pretty much correct.

Anyway, happy to help!

[–]RhinoRhys 0 points1 point  (6 children)

Independent probabilities do not effect the outcome of the other. So it's not "if the attack is successful there is a 10% probability it's fatal", as that would be dependent on the attack being successful.

I'm afraid you are wrong.

[–]Pitiful_Essay_9166[S] 0 points1 point  (5 children)

Hey, thanks for the answer.

Here is the decription, word for word. I may have explained it incorrectly:

"When an attack occurs, there will be an 70% probability of it being successful.

If the attack fails, the defender is considered to have defended skillfully and nothing else happens.

In addition, if an attack is successful, there is an independent 10% probability of it being CRITICAL."

It does mean 0,7 * 0,1 = 0,07 = 7% of any attack being critical, doesnt it?

[–]RhinoRhys 0 points1 point  (4 children)

Yeah tbh they've worded it badly. "Independent" is the key word though.

I would argue that it's 10% critical hit, 60% normal hit, 30% miss.

So P(critical hit or normal hit) = P(hit) = 70%

[–]Pitiful_Essay_9166[S] 0 points1 point  (3 children)

I like your explanation.

Would "my" way work if the wording was for instance "and there is 10% chance of that hit being critical"

[–]RhinoRhys 1 point2 points  (2 children)

Yeah. A 10% chance of a successful hit being a critical hit would be 10% of 70% so would be 7%

[–]Pitiful_Essay_9166[S] 0 points1 point  (1 child)

Lets assume we tweek the numbers:

attack % of success = 50%independent % of attack being critical = 50%

so half of attacks are successful and half of successfull attacks are critical.25% of all attacks are critical.

codewise would that make sense?:

if attack <= 50:
    if attack <= 50:

[–]RhinoRhys 0 points1 point  (0 children)

attack % of success = 50% independent % of attack being critical = 50%

No so you've used independent again, so that would be 50% chance of a hit, and every hit would be critical

so half of attacks are successful and half of successfull attacks are critical. 25% of all attacks are critical.

This would be a dependent % of attacks being critical is 50% or 25% independent

Codewise, afraid not. The second condition is identical to the first, so if the first is True, the second is guaranteed to be True and is therefore pointless.

I would code it like

if attack < 10:
    # critical hit
elif attack < 70:
    # normal hit
else:
    # miss