Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Array
- def sum
- inject(0.0) { |result, el| result + el }
- end
- def mean
- sum / size
- end
- end
- def flip
- if (rand * 2).ceil == 1
- 'heads'
- else
- 'tails'
- end
- end
- def simulate_alice
- alice_tries = []
- alice_not_found = true
- while alice_not_found
- alice_tries << flip
- if alice_tries.count >= 2 && alice_tries[-2] == 'heads' && alice_tries[-1] == 'tails'
- alice_not_found = false
- end
- end
- alice_tries.count
- end
- def simulate_bob
- bob_tries = []
- bob_not_found = true
- while bob_not_found
- bob_tries << flip
- if bob_tries.count >= 2 && bob_tries[-2] == 'heads' && bob_tries[-1] == 'heads'
- bob_not_found = false
- end
- end
- bob_tries.count
- end
- puts 10000.times.map { simulate_alice }.mean
- puts 10000.times.map { simulate_bob }.mean
Add Comment
Please, Sign In to add comment