pokemon-showdown/test/simulator
Matthew Glazar 45a876917d Refactor random booleans into randomChance function
Often, you just need a random boolean. Throughout Pokemon Showdown's
code, there are many creative ways of requesting random booleans. For
example:

    if (this.random(10) < 3) {
    if (this.isWeather(['sunnyday', 'desolateland']) || this.random(2) === 0) {
    let shiny = !this.random(1024);
    if (uberCount > 1 && this.random(5) >= 1) continue;
    if (!this.random(3)) ability = ability1.name;
    } else if ((ability === 'Iron Barbs' || ability === 'Rough Skin') && this.random(2)) {
    if (typeof secondary.chance === 'undefined' || this.random(256) <= effectChance) {
    if (accuracy !== true && this.random(256) > accuracy) {

Enable these methods to converge by introducing the PRNG#randomChance
function. It accepts a probability and returns true with that
probability.

Run the following sed script to refactor many common patterns to use
randomChance:

    s/this\.random(\([0-9]\{1,\}\)) >= \([0-9]\{1,\}\)/!this.randomChance(\2, \1)/g
    s/this\.random(\([0-9]\{1,\}\)) < \([0-9]\{1,\}\)/this.randomChance(\2, \1)/g
    s/this\.random(\([0-9]\{1,\}\)) === 0/this.randomChance(1, \1)/g
    s/!this\.random(\([0-9]\{1,\}\))/this.randomChance(1, \1)/g

The sed script takes advantage of the following properties:

    random(x) < y     is equivalent to   randomChance(y, x)
    random(x) >= y    is equivalent to   !(random(x) < y), i.e. !randomChance(y, x)
    random(x) === 0   is equivalent to   random(x) < 1, i.e. randomChance(1, x)
    !random(x)        is equivalent to   random(x) === 0, i.e. randomChance(1, x)

This commit should not change behaviour. In particular, PRNG#next is
called the same number of times with the same number of parameter as
before this commit, and PRNG#next's results are interpreted in the same
way as before this commit.
2018-03-08 20:11:33 +09:00
..
abilities Tests: Refactor more tests to use makeChoices() (#4325) 2018-01-24 14:56:58 -06:00
items Extend Protective Pads unit tests (#4467) 2018-03-08 04:11:37 +09:00
misc Refactor random booleans into randomChance function 2018-03-08 20:11:33 +09:00
moves Tests: Refactor more tests to use makeChoices() (#4325) 2018-01-24 14:56:58 -06:00