Reputation: 2085
I want to generate the most probable number depending on the latest lottery results I have on a CSV.
I have this script:
h = Hash.new
f = File.open('numbers.csv', "r")
f.each_line { |line|
numbers = line.split
numbers.each { |w|
if h.has_key?(w)
h[w] = h[w] + 1
else
h[w] = 1
end
}
}
# sorteamos el hash por valor, y lo pintamos según la concurrencia
h.sort{|a,b| a[1]<=>b[1]}.each { |elem|
puts "\"#{elem[0]}\" tiene #{elem[1]} concurrencia"
}
That will tell me which numbers have the most ocurrencies. I want to "sample" a number depending on these results that has the most probability.
How can I achieve this? Thanks!
Upvotes: 0
Views: 78
Reputation: 22375
I don't think Ruby has a built-in elegant way of doing this. You can treat your hash as a bunch of bins, where the number of occurrences of each number is the bin size. Then you can calculate the total bin width, get a random sample, and iterate to find out which bin the sample lands in.
def weighted_sample h
weight = h.values.reduce(:+)
sample = rand weight
h.each do |n, w|
return n if sample < w
sample -= w
end
end
Array.new(10) { weighted_sample({1 => 8, 2 => 4, 3 => 2}) }
# [2, 2, 1, 1, 1, 3, 1, 1, 1, 1]
Tip: there's a much nicer way to build that hash
h = Hash.new 0
# ...
h[w] += 1
Upvotes: 2