Last active
March 13, 2016 08:33
-
-
Save k-hamada/ba27e070b729d60f936f to your computer and use it in GitHub Desktop.
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
| module Lottery | |
| class << self | |
| def draw(probablity_rates, upper_limit = 10000) | |
| chosen_value = rand(0...upper_limit) | |
| probablity_rates.find_index{|probablity_rate| | |
| chosen_value -= probablity_rate | |
| chosen_value < 0 | |
| } | |
| end | |
| end | |
| end |
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
| rates = [1000, 2000, 3000, 4000] | |
| result = {} | |
| 10000.times do | |
| index = Lottery.draw(rates, rates.inject(:+)) | |
| result[index] ||= 0 | |
| result[index] += 1 | |
| end | |
| result.keys.sort.each do |k| | |
| puts "#{k}, #{result[k]}" | |
| end | |
| __END__ | |
| 0, 984 | |
| 1, 1966 | |
| 2, 3041 | |
| 3, 4009 |
Author
k-hamada
commented
Mar 13, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment