It uses numbers between 0 and 1, so you're going to encounter rounding problems, which already creates inaccuracies. These inaccuracies are further reinforced by Math.floor operations.
I could be misunderstanding you, but I don't see a good reason why either of those things should matter in this situation.
Firstly, being a number between 0 and 1 shouldn't necessitate any rounding, since it's essentially a ratio. You can just scale it up, and then it's no longer a number between 0 and 1. For example, the ratio 0.75 is equivalent to 75%, even though one is a number between 0 and 1 and the other is a number between 0 and 100. All that matters is that they can be broken up into the same number of individual units. I.e: 0.01 / 1 = 1 / 100 = 10 / 1000 = 100 / 10000, etc. Sorry, I probably didn't explain that well, but I'm not a math teacher, so I don't know how to explain it any better than that.
And the
Math.floor operation that is generally used in the process of converting the number isn't any type of detriment, since the algorithm properly accounts for (and actually relies on) it.
JavaScript:
Math.randomInt = function(max) {
return Math.floor(max * Math.random());
};
Supposing that you want to randomly generate either a 0 or a 1, you would supply an argument of 2 to the above function. I'll just manually write out some possible scenarios, pretending that Math.random returns a floating point number with only two digits after the decimal:
Code:
Math.random Result | Result after (max * Math.random) | Result after Math.floor |
-------------------|----------------------------------| ------------------------|
0.00 | 2 * 0.00 = 0.00 | 0 |
-------------------|----------------------------------| ------------------------|
0.10 | 2 * 0.10 = 0.20 | 0 |
-------------------|----------------------------------| ------------------------|
0.20 | 2 * 0.20 = 0.40 | 0 |
-------------------|----------------------------------| ------------------------|
...
0.49 | 2 * 0.49 = 0.98 | 0 |
-------------------|----------------------------------| ------------------------|
0.50 | 2 * 0.50 = 1.00 | 1 |
-------------------|----------------------------------| ------------------------|
0.60 | 2 * 0.10 = 1.20 | 1 |
-------------------|----------------------------------| ------------------------|
0.70 | 2 * 0.70 = 1.40 | 1 |
-------------------|----------------------------------| ------------------------|
...
0.99 | 2 * 0.99 = 1.98 | 1 |
-------------------|----------------------------------| ------------------------|
I don't know if I depicted that in a way that makes sense at all, but basically... any time
Math.random() generates a value from 0.00 to 0.49, the end result is 0. Any time it generates a value from 0.50 to 0.99, the end result will be 1. This means that (in this example) there are exactly 50 possibilities that can yield a 0, and exactly 50 possibilities that can yield a 1, which in turn means that there is a proper 50% chance to get either a 0 or a 1 (provided that the internal implementation of
Math.random() does yield an approximately uniform distribution). As you can see in the table,
Math.floor doesn't hurt the precision of the result--rather it's a key component of the algorithm.
Of course, the number returned by
Math.random() actually has a much higher precision than only two digits after the decimal, but that doesn't matter. Regardless, exactly half of the possible numbers that it returns are capable of yielding 0 as an end result, and exactly half are capable of yielding 1... so it's still a 50% chance. I simply went with two digits because it made the example easier to write and (presumably) read.
Just keep in mind that division and modulo are the slowest arithmetic operations and are slower than if else, so don't do a million divisions at once.
Sorry if this is pedantic, but just as an FYI, If the second operand is a power of 2, then it might actually be pretty fast, because there are some simple bit operations that can be used as shortcuts. For example, for modulo 2, you can actually just do a bitwise AND 1. In other words:
And for division by 2, you just have to shift each bit one to the right (>> 1). However, I don't know whether or not V8 is smart enough to automatically make those optimizations, so you could do the bit operations manually to be on the safe side.
Rather than binary 0 or 1, I changed it to a 0 or 99, and the conditional is just checking for 49 or less for one option and 50 or more for the other option.
It shouldn't matter. Internally, JavaScript is generating a random number between 0 and 1 anyway, and you're just scaling it up after the fact.
I wrote a really simple test. Playtest your game, press F12, click on the Console tab, paste this code and press enter:
JavaScript:
(() => {
const numTrials = 100000;
let armors = 0;
let weapons = 0;
for (let i = 0; i < numTrials; i++) {
if (Math.randomInt(2) == 0) {
armors++;
} else {
weapons++;
}
}
console.log(`Armors = ${armors}\nWeapons = ${weapons}`);
console.log(`Armor percent = ${armors / numTrials * 100}%`);
})();
What it will do is randomly generate either a 0 or a 1 (essentially the same way that RPG Maker would do it in a Control Variables: Random 0~1 event command) 100000 times, keep track of the frequency of each number, and tell you how many of each at the end (0 = Armors and 1 = Weapons). Just for reference, here are the results of me running it twice:
Code:
/* First run */
Armors = 49855
Weapons = 50145
Armor percent = 49.855%
/* Second run */
Armors = 50123
Weapons = 49877
Armor percent = 50.123%
To me, it doesn't seem that there's anything blatantly egregious going on. It would vary more widely if you're only running like 10 trials instead of 100000 (you can change the number of trials by changing the 100000 in the second line of code), but that's just how probability works. Of course, proper statistical testing of PRNG is a lot more complicated than this simple test, but the PRNG in a video game doesn't exactly have to be cryptographically secure (and for performance reasons, it probably shouldn't be), so I think this is good enough.
IMO, your initial playtests either had really crazy luck, or you may have a bug in your implementation.