4 ms·
It's very easy to verify experimentally that e is likely the correct answer.
by DanWaterworth 14y ago
It's very easy to verify experimentally that e is likely the correct answer.
- ced 14y agoI confirmed it for fun. Got 2.746 over 10000 trials. Close enough. (average (map-n (fn () (1+ (position-if (let1 x 0 (fni (< 1.0 (incf x (random 1.0))))) (range 100)))) 10000))
- psyklic 14y agoDoubly confirmed. 2.71832 over 100M trials. import random totalsteps = 0 for i in range(100000000): sum = 0.0 steps = 0 while sum < 1.0: sum += random.random() steps += 1 totalsteps += steps print('AVG STEPS: ' + str(totalsteps / i))
- DanWaterworth 14y agoThis is almost exactly what I wrote, I've done more than 4 billion trails and I have: 2.7182718267 (pypy speeds it up a lot).
- crististm 14y ago:) I was too fast for my own good and ddos-ed my box with this loop; it took me 5 minutes to stop it and replace range with xrange
- zwegner 14y agoThat's because you're using an obsolete Python :)
- Natsu 14y agoIt's funny. I read the problem, estimated that it was "probably 2 or 3" and then wasn't all that surprised to find that the solution was e. The derivation of it is pretty cool, too.
- prezjordan 14y agoYep: http://codepad.org/gIaWdVY9 http://codepad.org/gIaWdVY9 EDIT: Beaten to it :)
- eCa 14y agoConfirmed again: perl -e 'my $testcount = 10_000_000; my $total = 0; foreach my $i (0 .. $testcount) { my $sum = 0; while($sum < 1) { $sum += rand(); ++$total } } print $total / $testcount' 2.7186292