Hello everyone!
Today, I received my ATtiny in the post. (For those that do not know, it is an 8 pin microcontroller that you can program with arduino). I followed the tutorial here: http://hlt.media.mit.edu/wiki/pmwiki.php?n=Main.ArduinoATtiny4585 for programming it, and started having fun! One thing I realised before I bought the IC though, is that it did not have a random number generator function. In some applications, this does not matter. In others though, it does. With the ATtiny's small size, you might want to put it into a fake candle, or some other thing like that. A fake candle usually uses random number generators though, so it would not work how you might have wanted. For that reason, I built this:
unsigned int randomNumber = 5;
unsigned int previousRandomNumber;
int displayNumber;
float x = 2;
int randomNumber1;
int randomNumber2;
void setup(){
//Serial.begin(9600);
}
void loop(){
previousRandomNumber = randomNumber;
randomNumber = previousRandomNumber % 10;
randomNumber = randomNumber + x;
x = randomNumber;
randomNumber = (randomNumber % 10) +1;
randomNumber1 = randomNumber;
previousRandomNumber = randomNumber;
randomNumber = (previousRandomNumber * x);
if(!randomNumber) randomNumber = 1;
x = randomNumber;
randomNumber = (randomNumber % 10) +1;
randomNumber2 = randomNumber;
/*
Serial.print(randomNumber1);
Serial.print(" ");
Serial.println(randomNumber2);
*/
delay(100);
}
It generates a random number between 1 and 10, only using functions compatible with the ATtiny. You will notice that there are serial communicaion features commented out. By un-commenting them, you can test it on the uno, mega, duemillanove, or something similar. The ATtiny does not have serial communication features (that I know of!), so that is why I have commented them out. The first few random numbers generated can be seen here:
8 7
4 3
6 1
2 3
6 1
2 3
6 3
6 9
8 1
2 7
4 7
4 3
6 1
2 7
4 5
10 7
4 7
4 1
2 7
4 3
6 1
2 3
6 3
6 9
8 3
6 3
6 1
2 3
6 9
8 9
8 5
10 5
By changing the line randomNumber = (randomNumber % 10) +1; to randomNumber = (randomNumber % 100) +1; you can get a much bigger range of numbers. the first few are shown here:
64 33
36 89
98 51
52 57
64 73
76 73
76 37
44 5
10 83
86 55
60 53
56 9
18 7
14 83
86 31
32 29
38 27
34 7
14 11
12 45
50 43
46 67
74 99
8 77
84 33
36 45
50 67
74 19
28 53
56 65
70 63
66 23
26 63
66 47
54 3
6 23
26 15
Hopefully someone can make something useful of this entirely home-brew algorithm. Note that it was made by slowly adding bits, so there might be things in the code that are not used, like displayVal.
Onions.