News for hackers. Stuff that matters.
Re:Ask HN: How many times will this loop run on average? by hardlianotion on Sunday December 31, 2023 @03:28PM (#38827250)
Expected index given by

E [X] = \sum_{i \in [2,100)} p(X < i)\prod_{j < i}p (X \ge j)i.

Edit: Sorry rest of reply was wrong. Had to account for not hitting until i^th loop.

Re:Ask HN: How many times will this loop run on average? by aalhour on Sunday December 31, 2023 @03:36PM (#38827333)
Not sure that adds up, I translated the right side of the equality into a Python statement and it returns a number I am not sure how to interpret:

  1/99 * sum([(i-1)*i for i in range(2, 100)])   # 3266.666666666667
Both the median and mean are around 12 for 10K runs of the loop.
Re:Ask HN: How many times will this loop run on average? by hardlianotion on Sunday December 31, 2023 @03:47PM (#38827401)
I edited my response with an apology! Thanks for checking!
Re:Ask HN: How many times will this loop run on average? by aalhour on Sunday December 31, 2023 @03:57PM (#38827454)
No worries, I think your solution coverges with @Someone's above, if I am not mistaken. You're multiplying the probabilities for all values of j up to i for all values of i.
Re:Ask HN: How many times will this loop run on average? by hardlianotion on Sunday December 31, 2023 @04:06PM (#38827524)
Yes I am and yes I think it does