Close

RNG

A project log for Tiny Bit Machine

An 8-bit solar powered gadget.

g0730ng0730n 09/06/2024 at 16:210 Comments

To generate psuedo random numbers for use it the game, I found a method that seems like it will work great for the randomness I need.

Every time a button is pressed, we get the LSB of current millis() timestamp.

Our variable "int8_t random_number" is our global variable for storing the current random number.

First we shift bits 1 position left.

Then XOR the LSB

Then apply the variable to bitmask 127, which will guarantee the MSB is never a 1. We don't want any negative numbers stored in this variable.

Considering a button will be pressed every time any action is done in game, the generator will generate a new number every time a button is pressed, then we can read that number for applying to our game code.

Discussions