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.6const int lane = randomInt(1, 3); // 1, 2 or 3if (chance(0.1f)) spawn<PowerUp>(x, y, "Instancias"); // 10% chancesprite = pick("inimigo_verde", "inimigo_roxo"); // one of the twoHow it works
Section titled “How it works”| 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. |
Variation: the same result every time
Section titled “Variation: the same result every time”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.
