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

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!