public
Last active

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
class Fairish
def initialize(probability, unfair_low, unfair_high, min_rolls)
@probability, @unfair_low, @unfair_high, @min_rolls = probability, unfair_low, unfair_high, min_rolls
@rolls, @hits = 0, 0
end
 
def fire!
hit = if @rolls >= @min_rolls && observed_probability > @unfair_high
false
elsif @rolls >= @min_rolls && observed_probability < @unfair_low
true
else
rand <= @probability
end
@hits += 1 if hit
@rolls += 1
return hit
end
 
private
def observed_probability
@hits.to_f / @rolls
end
end
 
class Random
def initialize(probability)
@probability = probability
end
 
def fire!
rand <= @probability
end
end
 
trials = 100000
trial_size = 10
random_out_of_bounds_low = 0
random_out_of_bounds_high = 0
fairish_out_of_bounds_low = 0
fairish_out_of_bounds_high = 0
 
probability = 0.2
lower_bound = 0.1
upper_bound = 0.4
min_rolls = trial_size / 2
 
trials.times do |n|
fairish = Fairish.new probability, lower_bound, upper_bound, min_rolls
fairish_hits = 0
random = Random.new probability
random_hits = 0
trial_size.times do |x|
fairish_hits += 1 if fairish.fire!
random_hits += 1 if random.fire!
end
 
fairish_out_of_bounds_low += 1 if fairish_hits.to_f / trial_size < lower_bound
fairish_out_of_bounds_high += 1 if fairish_hits.to_f / trial_size > upper_bound
random_out_of_bounds_low += 1 if random_hits.to_f / trial_size < lower_bound
random_out_of_bounds_high += 1 if random_hits.to_f / trial_size > upper_bound
end
 
puts "Fairish was out of bounds #{(fairish_out_of_bounds_low + fairish_out_of_bounds_high).to_f/trials * 100}% of the time (#{fairish_out_of_bounds_low.to_f/trials * 100}% low and #{fairish_out_of_bounds_high.to_f/trials * 100}% high)."
puts "Random was out of bounds #{(random_out_of_bounds_low + random_out_of_bounds_high).to_f/trials * 100}% of the time (#{random_out_of_bounds_low.to_f/trials * 100}% low and #{random_out_of_bounds_high.to_f/trials * 100}% high)."

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.