News for hackers. Stuff that matters.
Re:Weighted/Biased Random Number Generation with JavaScript ... by mrcactu5 on Monday April 22, 2013 @11:00AM (#5589530)
You can take relative weights (a,b,c) and take the partial sums:

(0,a,a+b,a+b+c)

random() gives values in [0,1], so divide your numbers by a + b + c.

Generating random structures is the tip of a very big iceberg.

Re:Weighted/Biased Random Number Generation with JavaScript ... by jere on Monday April 22, 2013 @11:41AM (#5589822)
I came up with something pretty similar while generating items. However, the following code gets pretty hard to read with lots of items because the values don't line up. And while balancing, you will want to be able to read and modify it very easily:

>var list = ['javascript', 'php', 'ruby', 'python']; var weight = [0.5, 0.2, 0.2, 0.1];

You could instead put each into an object: { language: 'javascript', weight: 0.5 }

My code (in the itemRoll function) demonstrates this, but it is a little bit more rolled out because I was instantiating objects: http://humbit.com/rogue/afsahr-v1.2/js/inventory.js

Re:Weighted/Biased Random Number Generation with JavaScript ... by dllu on Monday April 22, 2013 @01:14PM (#5590457)
For method 2, if you precompute the cumulative distribution function, you can do a binary search which is only O(log N) instead of O(N).
Re:Weighted/Biased Random Number Generation with JavaScript ... by BadCRC on Monday April 22, 2013 @12:15PM (#5590069)
why not have a dynamic multiplier based on the weights? rather than just 100x