Random
trigscript/random is chance in the words a map maker uses: one time in four, a number
from 1 to 3, one of a list.
import { chance, range, pick, weighted } from "trigscript/random";
const BOSSES = [units.ZergUltralisk, units.ZergDefiler, units.ZergQueen];
program(async () => {
let wave = 1;
while (wave <= 10) {
createUnit(P8, units.ZergZergling, range(4, 8), locations.Spawn);
if (chance(25)) createUnit(P8, pick(BOSSES), 1, locations.Spawn);
const tier = weighted([70, 25, 5]); // 0 most of the time, 2 rarely
setResources(P1, "add", 50 * (tier + 1), "ore");
wave += 1;
await sleep(seconds(30));
}
});
The functions draw on the game's own random numbers, which every player's game draws
alike, so a game of several players never goes out of step. Random is a generator of
your own, seeded: the same seed gives the same numbers in every game. That is what a level
made from a seed, or a code a player types to get the same map again, needs.
import { Random } from "trigscript/random";
program(() => {
const rng = new Random(1234);
const lanes = [0, 1, 2, 3];
rng.shuffle(lanes);
print(`Lanes in order: ${lanes.join(", ")}, first gap at ${rng.range(5, 20)}`);
});
| Name | What it gives |
|---|---|
chance(percent) |
True percent times in a hundred. |
range(min, max) |
A whole number from min to max, both included. |
dice(count, sides) |
The total of count dice of sides sides: dice(3, 6) is 3 to 18. |
pick(items) |
One of a list, any one alike. |
weighted(weights) |
The place of one of the weights, each as likely as its share: [70, 25, 5] gives 0 most of the time. |
shuffle(items) |
An array of the program in a random order, in place. |
new Random(seed) |
A seeded generator with next(), int(n), range(min, max), chance(percent), pick(items) and shuffle(items). |