Randomness and noise
Two operations about unpredictability: one different every time, the other smooth
rand gives a random number from zero up to but not including the one you give, and that
number is fractional. A die comes out of it in two lines: first the throw, then rounding down.
op rand throw 6 gives a fractional number below six — say 2.09; floor cuts the fraction and
leaves 2; add one and you get 1 to 6, like a real die. Press “step” a few times: the throw is
different every time.
The order matters here. Round rand 5 first and then add, and a six never comes up at all;
take rand 7 and round it, and a seven turns up now and then. An N-sided die is always
rand N, floor, add 1.
In the example the numbers are always the same
Section titled “In the example the numbers are always the same”Press “restart” and run the example again — the same thing comes up. The game is not like that: there the generator is seeded randomly at start-up, and the same program gives different numbers every time.
Here the seed is fixed on purpose: the lesson promises numbers, the tests check them, and an example that shows something different every time can teach nothing. Keep that in mind when you move a program into the game: there will be no repeats there.
Noise is not randomness
Section titled “Noise is not randomness”noise takes two coordinates and gives a number roughly between −1 and 1. It is not
random: the same point always has the same answer, and neighbouring points have nearly the
same one.
In the example the points (10, 10) and (10.01, 10) gave −0.524 and −0.520 — neighbours, and their answers are close together. (40, 10) is far away, and the answer there is quite different: 0.908.
That is exactly what makes noise useful. A random number jumps about, and anything built on it flickers; noise changes smoothly, so hills, ore patches and stains on a map come out of it — everything that should look uneven but not torn. The game draws terrain with it, and in logic it is handy for a smoothly wandering value: a speed, a delay, a brightness.
Unofficial fan project, not affiliated with Anuke. Mindustry sprites, fonts and translations © Anuke, used under GPL-3.0; Fira Code under OFL-1.1. Site code is GPL-3.0.