pokemon-showdown/test/simulator/misc
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
..
arceus.js Cache species-specific effects (e.g. Arceus); 20% overall perf win (#4459) 2018-03-05 17:24:14 +09:00
choice-parser.js Fix getChoice 2018-01-29 20:59:52 -06:00
decisions.js Fix getChoice 2018-01-29 20:59:52 -06:00
dex.js
eventemitter.js Make eslint rules stricter 2017-10-08 04:41:11 -05:00
faint-order.js Test faint order 2017-12-01 13:06:01 -06:00
inversebattle.js Make Inverse Battle test work without a format 2017-12-01 23:11:31 +08:00
megaevolution.js Fix certain Abilities not being overwritten by most forme changes (#4245) 2017-12-14 11:45:12 -05:00
mixandmega.js Fix certain Abilities not being overwritten by most forme changes (#4245) 2017-12-14 11:45:12 -05:00
prng.js Refactor random booleans into randomChance function 2018-03-08 20:11:33 +09:00
random-teams.js Tests: Fix typo 2018-03-02 14:32:28 +00:00
statuses.js Use PRNG in sim/ files (#4365) 2018-01-24 12:27:07 -06:00
statusmoves.js
trapmoves.js Refactor old gens to for...of (#4274) 2017-12-23 21:46:04 -06:00
turn-order.js Implement Ultra Burst 2017-11-17 00:01:09 -06:00
typechange.js
weather.js
weight.js