I have, for example, this table
+-----------------+ | fruit | weight | +-----------------+ | apple | 4 | | orange | 2 | | lemon | 1 | +-----------------+
I need to return a random fruit. But appleapple should be picked 4 times frequently then lemonas frequent as Lemon and 2 times frequently then orangeas frequent as orange.
In more general case it should be f(weight) times frequently.
What is a good general algorithm to implement this behavior?
Or maybe there isare some ready gems on Ruby? :)
PS
I've implemented current algorithm in Ruby https://github.com/fl00r/pickup