I am looking for suggestions to create a truly random 256 bit integer in the smallest possible progmem space. Here is what I currently have. I am using the generateRandomSeed()
function from the Arduboy library plus 8 button presses on any of the four black keys. The highest byte of generateRandomSeed()
is not random, because ADC only has 10 bits, and the lowest two bits are always zero, because micros() is always a multiple of 4 on the Arduboy. I fix the least significant bits by adding two bits based on which button was pressed. I fix the highest byte by calling generateRandomSeed()
a second time, shift it up by 8 bits and xor it with the rest. From a randomness point of view I do think this is pretty good, but I am sure it can be made smaller in terms of progmem use.
uint32_t x[8];
rand256(x);
void rand256(uint32_t* a) {
int8_t i=0;
while (i < 8) {//8 button presses are turned into 32 bytes of entropy
arduboy.pollButtons();
if (arduboy.justPressed(UP_BUTTON) | arduboy.justPressed(DOWN_BUTTON) | arduboy.justPressed(LEFT_BUTTON) | arduboy.justPressed(RIGHT_BUTTON)){
a[i] = arduboy.generateRandomSeed();
if (arduboy.justPressed(DOWN_BUTTON)) {a[i] += 1;}
if (arduboy.justPressed(RIGHT_BUTTON)) {a[i] += 2;}
if (arduboy.justPressed(LEFT_BUTTON)) {a[i] += 3;}
a[i] ^= arduboy.generateRandomSeed() << 8; //introduce entropy at highest byte
i++;
}
}
}