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 an 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 the test with a 20% 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 a user-defined distribution with a trigonometric function across 91 discrete values, however, the result does not fit very well in 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
source share