I use two different methods of randomization, while one gives the results, the variance of which I expect, the other method gives the results, distorted and very consistent.
Methods:
function randomA() {
var raw = Number((Math.random()+'').substr(2));
return raw % NUM_OF_POSSIBLES;
}
function randomB() {
var raw = Math.round(Math.random()*10000);
return raw % NUM_OF_POSSIBLES;
}
When the NUM_OF_POSSIBLES = 2first method ( randomA()) leads to a fairly constant number of zeros (64%) and 36% of 1s. While radnomB()doing quite a lot of 50/50. If NUM_OF_POSSIBLES = 5, the first method is again distorted in a rather consistent way: 0: 10%, 1: 23%, 2: 22%, 3: 22%, 4: 23%, and the second gives about 20% to each result.
Here you can find the full code with several tests: jsfiddle
Why is the first method skewed, and also why a consistent skew?