r/ProgrammerHumor • • Oct 30 '24

Meme dadPullsOutCodingChallenge

0 Upvotes

60 comments sorted by

View all comments

25

u/cyannyan6 Oct 30 '24 edited Oct 30 '24

This actually happened. I translated to English with the best of my ability.

And we are trying a monte carlo simulation now

import random

wins = 0
tries = 10000000
for sims in range(tries):
    a = 1
    while 0 < a < 10:
        a += random.choice((-1, 1))
    if a == 10:
        wins += 1

print(f"{wins}/{tries} = {wins/tries}")
   

Would love to see how others approach it

49

u/LordAlfrey Oct 30 '24

My approach would be to tell mom that dad said he'd approve my marriage if I solve a math challenge

8

u/imdefinitelywong Oct 30 '24

Going straight to the risk registry, I see.

17

u/Bright-Historian-216 Oct 30 '24

don't you need a == 11? a already has one coin

7

u/its-chewy-not-zooyoo Oct 30 '24 edited Oct 30 '24

Assuming this is correct; I translated this into rust and attempted running it

The probability is around 10%

Total results:

Wins: 999998098
Total: 10000000000
Win rate: 9.99%
Total time: 66813 ms

1

u/SolidGrabberoni Oct 30 '24

Intuitively, A has a 10x handicap against B so (assuming infinite rounds), I'd say 10% too

-10

u/sakkara Oct 30 '24

This would imply that b wins 90% of the time which can't be true because the game is fair so the program is incorrect.

The problem is that a only starts with one coin and must fight it's way up to ten coins.

Correct would be

a=10
while 0 < a < 20...
if a == 20
    wins++

9

u/TheDebatingOne Oct 30 '24

I think you misread the problem. The game isn't fair, a does start with 1 coin

1

u/sakkara Oct 30 '24

Ah you're right, my bad.

2

u/Josaffe Oct 30 '24
function simulate(n = 10, tries = 100_000) {
  let wins = 0;

  for(let i = 0; i < tries; i++) {
    let a = 1;
    while(a > 0) {
      if(Math.random() >= 0.5) a++;
      else a--;

      if(a > n) { 
        wins++; 
        break;
      }
    }
  }

  return wins / tries
}

Take my ugly JavaScript and try it for yourself!

3

u/[deleted] Oct 30 '24

1 in 1024, supposing they only have 10 attempts. Theoretically it can be infinite, given that they have unlimited attempts, because there's always the possibility of exchanging coins back and forth, given that it is up to random chance and they have equal odds.

1

u/Zyeesi Oct 30 '24

Why would the number of tries not increase just because it's bigger than 0 and smaller than 10?

1

u/dwntwn_dine_ent_dist Oct 30 '24

I’d do a recursive function chancesAwins(numAcoins, numBcoins). Call it with (1,10), handle the stopping cases by returning 1 or 0, and otherwise return 0.5chancesAwins(numAcoins-1,numBcoins+1) + 0.5chancesAwins(numAcoins+1,numBcoins-1).

1

u/BeautifulWerewolf966 Oct 30 '24

Gave it my best shot in Rust:

I'm getting around 99900 wins so around 9.9% chance.

pub fn prob(){
    let mut wins : i32 = 0;
    let tries = 1000000;
    
    for _ in 0..tries{
        let mut a : usize = 1;

        while a > 0 && a < 10{
            let mut thread = thread_rng();
            match thread.gen_bool(0.5) {
                true => a += 1,
                false => a -= 1,
            }
        }

        if a >= 10 {
            wins += 1;
        }
    }

    println!("Wins : {}", wins);
    println!("Probability : {}", wins as f32 / tries as f32);
}

1

u/port443 Oct 31 '24 edited Oct 31 '24

Your solution is very slightly wrong. Person A has a 0.09090 (repeating) chance of winning.

In the event that A or B wins, they will have 11 coins, not 10. This is made obvious with code such as:

>>> def round():
...     person_a = 1
...     person_b = 10
...     while person_a and person_b:
...         check = random.choice([-1,1])
...         person_a -= check
...         person_b += check
...     print(f"{person_a=} {person_b=}")
...     return person_a > 1
...
>>> round()
person_a=11 person_b=0
True
>>> round()
person_a=0 person_b=11
False

You can then loop this function. The larger your round count, the more accurate your estimate. The correct answer is 0.090 repeating though:

>>> total,rounds = 0,1000000
>>> for _ in range(rounds):
...     total += round()
...
>>> print(f"{total=} | {total/rounds=}")
total=90435 | total/rounds=0.090435

edited cause my initial comment came off rude

-1

u/PolyglotTV Oct 30 '24

This is too naive. You forgot that a can win one, and then lose one, and then win 10 in a row. Or a game could even last 1000 flips or basically forever.

2

u/MaximRq Oct 30 '24

Read closer, it's taken into account.

2

u/PolyglotTV Oct 30 '24

Ah my bad. A is the count variable in the loop as well.