#!/usr/bin/env ruby #========================================================================== # Initial author of this file: Martin.Vahi@softf1.com # This file is in public domain. #-------------------------------------------------------------------------- # The Bash line and the command line output: =begin ruby ./2017_10_29_randomness_and_the_e.rb 10000000 Histogram Column Label | Histogram Column Height ---------------------- | ----------------------- 0 | 0 1 | 0 2 | 5001315 3 | 3331379 4 | 1250445 5 | 333403 6 | 69654 7 | 11837 8 | 1736 9 | 208 10 | 22 11 | 1 12 | 0 13 | 0 14 | 0 15 | 0 16 | 0 more_than_max_bucket | 0 ------------------------------------------------ Approximate arithmetic mean of the n of selections: 2.7182336 ------------------------------------------------ =end #-------------------------------------------------------------------------- # According to Dr. Alexander Flint's tweet at # # https://twitter.com/neuroicudoc/status/924331454036811776 # (archival copy: https://archive.fo/uVpVa ) # # which I noticed due to the re-tweet of the Joanna Rutkowska # # https://twitter.com/rootkovska/status/924541577686634496 # (archival copy: https://archive.fo/ih4mB ) # # it takes about e=2.71828... # random number selections from the range of [0,1] # till the sum of those selections is greater than 1. # # This Ruby class is created to check that claim. class The_experiment def initialize @ht_histogram_of_the_number_of_selections_till_sum_is_greater_than_1=Hash.new @i_histogram_highest_numbered_bucket=16 # the lowest is 0 (@i_histogram_highest_numbered_bucket+1).times do |i| # i==0 at first iteration # As the random numbers are chosen from range [0,1], which # includes 1, but the sum must be greater than 1, then # the histogram buckets with labels 0 and 1 are guaranteed to # have 0 entries. However, to keep this Ruby code simpler, the # 0 and 1 are added to the histogram. No off-by-1-s at the lower end. @ht_histogram_of_the_number_of_selections_till_sum_is_greater_than_1[i]=0 end # loop @lc_s_more_than_max_bucket="more_than_max_bucket" # to avoid reinitialization of that string @ht_histogram_of_the_number_of_selections_till_sum_is_greater_than_1[@lc_s_more_than_max_bucket]=0 @lc_linebreak="\n" end # initialize private # (b_failed_to_top_1, i_n) def b_i_n_of_selections_till_the_sum_of_elections_is_greater_than_1() b_failed_to_top_1=true i_n=0 r_sum=0 r_0=nil while((r_sum<1)&&(i_n<@i_histogram_highest_numbered_bucket)) do i_n+=1 r_0=Random::rand().to_r r_sum+=r_0 end # loop b_failed_to_top_1=false if (1