I know that I can use
var rolls = [];
for (var i=0; i<100; i++) {
rolls.push(Math.floor(6 * Math.random()) + 1);
}
to get 100 rolls with a single die.
But what if it is a magic die, where each number doesn't show up equally?
So instead of each number showing up 1/6th of the time, the numbers 1-4 each shows up 10% of the time whereas 5 shows up 20% of the time and the remaining 6 shows up 100%-20%-4*10% = 40% of the time.
How do you make such a random number generator where the distribution can easily be adjusted?
You could use an array with probabilities and check and count against a random value.
This function sets first the return value to the last possible index and iterates until the rest of the random value is smaller than the actual probability.
The probabilities have to sum to one.