Guest User

Untitled

a guest
Jun 19th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. class Array
  2. def sum
  3. inject(0.0) { |result, el| result + el }
  4. end
  5.  
  6. def mean
  7. sum / size
  8. end
  9.  
  10. end
  11.  
  12. def flip
  13. if (rand * 2).ceil == 1
  14. 'heads'
  15. else
  16. 'tails'
  17. end
  18. end
  19.  
  20.  
  21. def simulate_alice
  22. alice_tries = []
  23. alice_not_found = true
  24. while alice_not_found
  25. alice_tries << flip
  26. if alice_tries.count >= 2 && alice_tries[-2] == 'heads' && alice_tries[-1] == 'tails'
  27. alice_not_found = false
  28. end
  29. end
  30. alice_tries.count
  31. end
  32.  
  33. def simulate_bob
  34. bob_tries = []
  35. bob_not_found = true
  36. while bob_not_found
  37. bob_tries << flip
  38. if bob_tries.count >= 2 && bob_tries[-2] == 'heads' && bob_tries[-1] == 'heads'
  39. bob_not_found = false
  40. end
  41. end
  42. bob_tries.count
  43. end
  44.  
  45. puts 10000.times.map { simulate_alice }.mean
  46.  
  47. puts 10000.times.map { simulate_bob }.mean
Add Comment
Please, Sign In to add comment