Hardware/software used for this project (read the parts list for a more comprehensive list of hardware):
- AVR-GCC toolchain
- My T85 Target board (click here for the project page for the details of the board)
- a breadboard with a photoresistor, some jumper wires and a resistor
I've used avr-size to determine that my compiled program uses less than 1KB for the HaD 1kB challenge.
Interesting things that I've learned so far:
The internal temperature sensor in the Attiny85 (connected to ADC4) for my purposes makes a better seed source for random number generation than an ADC pin that's unconnected. Another plus to using the internal temperature sensor is that random number generation is unaffected by having my programmer plugged in/not plugged into my development board.
Char can be used in place of int if you only need to deal with numbers <= 255 which helps save on RAM usage.
The rand() function takes up less space than random() which allowed my program to to be within the 1 KB challenge limit, but still takes up quite a bit of flash compared to the rest of my program's code.
Have you looked at a https://en.wikipedia.org/wiki/Linear-feedback_shift_register ?
It takes a tiny amount of code to implement, and gives a good pseudo random result.
http://www.cs.miami.edu/home/burt/learning/Csc609.022/random_numbers.html
.
Here's one I wrote awhile ago - the bit of interest is right at the end... the lfsr variable.
.
.
// Move into the pseudo random sequence by 255 numbers.
// This gets the variable nicely "warmed".
lfsr = 1;
for(; ++ctr < 255 ;)
lfsr = (lfsr >> 1) ^ (-(lfsr & 1u) & 0xd0000001u); // taps 32 31 29 1
.
.
//New random number... starts here.
//Fiddle the bits, then take the left hand few bits for the random number.
lfsr = (lfsr >> 1) ^ (-(lfsr & 1u) & 0xd0000001u); // taps 32 31 29 1
.
//Get two random numbers from the most recent bit twiddling.
newRandom1 = unsigned char)(lfsr >> 20) & 31 // Take the 20th to 24th bit for the timer. Range: 0-31
newRandom2 = (unsigned char)(lfsr >> 24) & 255); // and 24th to 29th bit for the brightness. Range: 0-63