Randomness and PETI: Understanding the PETI PRNG
Zac Adam-MacEwen edited this page 2022-08-24 10:26:11 -03:00

Nota Bene: It is vitally important that you do not treat the random number generation included in the lib/game/entropy package as cryptographically secure. Even if the relevant bit lengths weren't laughably short for that purpose, the implementation is a toy implementation. You probably shouldn't use it where the stakes are any higher than creating random events within the context of a prizeless game.

Initial Random Seed

PETI sources an initial random seed to limit the predictability of the events controlled by the PRNG during the Hardware Initialization Process. Prior to setting up any ports for GPIO, the ADC is briefly engaged and set to sample for a short period, with the resulting integer output taken across a modulus of 8 and used to select a 16-bit word from the Random Number Seed, a unique value TI has burned seperately into each MSP430 device. This random 16-bit integer is then used as the input seed for the PRNG initial state.

Avoiding Manipulation: Rolling the RNG

As part of the main operating loop, the PRNG is triggered. Since the PRNG's output feeds back into the PRNG as well, this "advances" the state of the PRNG in a way that is difficult for a human to accurately track and predict, meaning that even in cases where the random seed is known or accurately predicted, it will be difficult to predict the state of the game's RNG.

Using Entropy: Drawing the RNG

For convenience, a non-void function for the RNG is also provided, which:

  • Performs the same calculation as the rolling function, including updating the PRNG internal state, and;
  • Returns the result as an integer for use in a scene or other functions.

Greater detail will be provided as implementation progresses.