In ruby I would do it like this:
class DistributedRandom
def initialize(left, right = nil)
if right
@distribution = [0] * left + [1] * right
else
@distribution = left
end
end
def get
@distribution[rand @distribution.length]
end
end
Running a test with 80:20 distribution:
test = [0,0]
rnd = DistributedRandom.new 80, 20 # 80:20 distribution
10000.times { test[rnd.get] += 1 }; puts "Test 1", test
Running a test with 20% more distribution on the right side:
test = [0,0]
rnd = DistributedRandom.new 100, 120 # +20% distribution
10000.times { test[rnd.get] += 1 }; puts "Test 2", test
Running a test with custom distribution with a trigonometric function over 91 discrete values, output however does not fit very well into the previous tests:
test = [0,0]
rnd = DistributedRandom.new((0..90).map {|x| Math.sin(Math::PI * x / 180.0)})
10000.times { test[rnd.get] += 1 }; puts "Test 3", test