Skip to content
Download

Random numbers

You want a bit of luck in the game: enemies at different positions, an item that sometimes shows up.

vspeed = random(0.8f, 1.6f); // a decimal number between 0.8 and 1.6
const int lane = randomInt(1, 3); // 1, 2 or 3
if (chance(0.1f)) spawn<PowerUp>(x, y, "Instancias"); // 10% chance
sprite = pick("inimigo_verde", "inimigo_roxo"); // one of the two
Function What it picks
random(a, b) A decimal number, from a up to almost b.
randomInt(a, b) A whole number from a to b, including both.
chance(p) true with probability p: 0.25f is 25%.
pick(a, b, c...) One of the values you pass.

To test a level, sometimes you want the “random” to repeat itself exactly. Fix the seed at the start:

randomSeed(42);

With the same seed, the sequence of random results is always the same.