Created
December 3, 2012 22:08
-
-
Save klappy/4198565 to your computer and use it in GitHub Desktop.
Heads or Tails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
samples = 100000.times.with_object([]){|i,a|a.push ["heads","tails"].sample} | |
#always choose heads | |
samples.map{|coin|coin=="heads"}.group_by{|o|o}.map.with_object({}){|h,o|o[h[0]]=h[1].count}[true].to_f/1000 | |
#alternate every other call | |
samples.map.with_index{|coin,i|coin==(i.even? ? "heads" : "tails")}.group_by{|o|o}.map.with_object({}){|h,o|o[h[0]]=h[1].count}[true].to_f/1000 | |
#randomly choose | |
samples.map{|coin|coin==["heads","tails"].sample}.group_by{|o|o}.map.with_object({}){|h,o|o[h[0]]=h[1].count}[true].to_f/1000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment