There's a really cool way to do this that I learned about with ruby here
<a href="https://gist.github.com/O-I/3e0654509dd8057b539a" rel="nofollow">https://gist.github.com/O-I/3e0654509dd8057b539a</a><p>Here's a quick demo class that shows the technique. It's amazingly simple.<p>class Demo
EXAMPLE = { "75%" => 0.75, "15%" => 0.15, "9%" => 0.09, "1%" => 0.01 }<p><pre><code> def self.sample(choices = EXAMPLE)
choices.max_by { |_, weight| rand ** (1.0 / weight) }.first
end
def self.show(samples: 10000)
items = samples.times.map {sample}
items.each_with_object(Hash.new(0)) do |item, counts|
counts[item]+=1
end.sort_by(&:last).reverse.to_ h
end
</code></pre>
end<p># Demo.show
# => {"75%"=>7480, "15%"=>1525, "9%"=>901, "1%"=>94}<p>Edit: Sorry for the bad formatting. I added a gist:<p><a href="https://gist.github.com/jasonl99/25d3c922d73f10a75fe228c2de38d270" rel="nofollow">https://gist.github.com/jasonl99/25d3c922d73f10a75fe228c2de3...</a>