Skip to content

Instantly share code, notes, and snippets.

@ifarkas
Created August 17, 2012 09:14
Show Gist options
  • Save ifarkas/3377279 to your computer and use it in GitHub Desktop.
Save ifarkas/3377279 to your computer and use it in GitHub Desktop.
Probability distribution of continuous stream of random numbers vs probability distribution of sporadic picks of a continuous stream of random numbers
EXPERIMENTS_COUNT = 100000
PICKED_MATCH_COUNT = 5
DISREGARD_PICK_PROBABILITY = 0.26
pg = ProviderSelection::PriorityGroup.new(0)
pg.matches << ProviderSelection::Match.new(nil, -100)
pg.matches << ProviderSelection::Match.new(nil, 0)
# Continuous stream of random numbers
result_continuous = []
1.upto(EXPERIMENTS_COUNT) do
result = Hash.new(0)
while(result.values.sum < PICKED_MATCH_COUNT)
result[pg.matches.find_index(pg.get_random_match)] += 1
end
result_continuous << result
end
# Sporadic picks of the continuous stream of random numbers
result_sporadic = []
1.upto(EXPERIMENTS_COUNT) do
result = Hash.new(0)
while(result.values.sum < PICKED_MATCH_COUNT)
# We are disregarding at least every other output of rand() since we are calling it in the condition
# How many of the remaining rand() calls we disregard is defined by DISREGARD_PICK_PROBABILITY
result[pg.matches.find_index(pg.get_random_match)] += 1 if rand > DISREGARD_PICK_PROBABILITY
end
result_sporadic << result
end
# Display the result
complete_prob_of_0 = result_continuous.sum{ |result| result[0] }.to_f / EXPERIMENTS_COUNT
complete_prob_of_1 = result_continuous.sum{ |result| result[1] }.to_f / EXPERIMENTS_COUNT
sporadic_prob_of_0 = result_sporadic.sum{ |result| result[0] }.to_f / EXPERIMENTS_COUNT
sporadic_prob_of_1 = result_sporadic.sum{ |result| result[1] }.to_f / EXPERIMENTS_COUNT
puts "Probability of picking the 1st in continuous: #{complete_prob_of_0}"
puts "Probability of picking the 2nd in continuous: #{complete_prob_of_1}"
puts "The ratio of the 2 pick: #{complete_prob_of_0.to_f / complete_prob_of_1}"
puts "Probability of picking the 1st in sporadic: #{sporadic_prob_of_0}"
puts "Probability of picking the 2nd in sporadic: #{sporadic_prob_of_1}"
puts "The ratio of the 2 pick: #{sporadic_prob_of_0.to_f / sporadic_prob_of_1}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment