ATtiny random number generator

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.

NIce try, did you do some frequency analysis if all numbers occur in ~same amount?

On wikipedia I found this code (~220 bytes).
The unsigned long result can be modulo-ed to any range and offset e.g r = getRandom() % 1024 -512;

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  Serial.println(getRandom());
}

// An example of a simple pseudo-random number generator is the 
// Multiply-with-carry method invented by George Marsaglia.
// two initializers (not null)
unsigned long m_w = 1;
unsigned long m_z = 2; 

unsigned long getRandom()
{
    m_z = 36969L * (m_z & 65535L) + (m_z >> 16);
    m_w = 18000L * (m_w & 65535L) + (m_w >> 16);
    return (m_z << 16) + m_w;  /* 32-bit result */
}

Hello Onions!

Today, I received my ATtiny in the post

Welcome to the club!

One thing I realised before I bought the IC though, is that it did not have a random number generator function

These functions should be available in your Sketch...

And should give about the same quality as the Marsaglia generator.

@Coding Badly

These functions should be available in your Sketch...

So they are... I thought that I could not use them, as it says at the bottom of the tutoial:

The following Arduino commands should be supported:

  • pinMode()
  • digitalWrite()
  • digitalRead()
  • analogRead()
  • analogWrite()
  • shiftOut()
  • pulseIn()
  • millis()
  • micros()
  • delay()
  • delayMicroseconds()

Oops! :blush:

@robtillaart
The random number generator you posted looks good. If I only looked on wikipedia first...
XD

Thanks everyone!
Onions

I thought that I could not use them, as it says at the bottom of the tutoial...

I think the list is just for "core" functions. Even then, I think it's incomplete. I believe that core includes analogReference.

Were I in your shoes, I wouldn't pay too much attention to that list.

The compiler as the random function, as part of the standart libs, so rand(); should compile without errors.

I think the list is just for "core" functions. Even then, I think it's incomplete. I believe that core includes analogReference.

Were I in your shoes, I wouldn't pay too much attention to that list.

Yes, I agree. It does seem in-complete.

The compiler as the random function, as part of the standart libs, so rand(); should compile without errors.

And it does (luckily!), so I will just use random(), not my code. :relaxed:

Late last night, I built a little board for my ATtiny. It is similar to the arduino, but it is programmed via an ISP - in my case, the arduino. It has all the pin connectors, like those found on the arduino. It also has a reset switch, but does not support USB programming, have any LEDs, or anything fancy like that. So, in some ways it is similar to the arduino, and in other ways it is very different.

I had some annoying troubles when I built it. First of all, I tested the reset switch to make sure I had it the right way round. I then put it onto the stripboard and soldered it in, the wrong way. After half an hour trying to de-solder it, I was left with a bad looking switch with bent legs. Eventually though, I put it in the right way.
Then, I picked up the IC mounting connector, then droppped it. I spent a while trying to find it, before giving up. This was very annoying, because I had to look very hard to find one in the first place. Convinced that I had just lost my only connector, I set about feeling annoyed.
Luckily for me though, I managed to find a new one.
The resulting board is very nice, and it was even better when I achived first time success in getting it to work! There is one problem with it. I set the reset pin connector off to a side, with the intensions of having it by the MISO, MOSI and SCK pin for easy programming. Somehow, I managed to put it at the opposite side. Oops!

I gave the completed board the very (un)imaginitive name of ATtiny Board. If anyone wants to suggest a better name, I would happily hear it!

For those with an interest in building it too, the schematic is here:

a bigger version is here:

Onions.

Thanks for the post!

The resulting board is very nice, and it was even better when I achived first time success in getting it to work!

Congratulations!

I gave the completed board the very (un)imaginitive name of ATtiny Board. If anyone wants to suggest a better name, I would happily hear it!

Onions' Tiny Bread.

For version II, include a 0.1 uF across +5 and GND. It seems to help "smooth" analog-to-digital conversions and, in general, it's a good idea...
http://www.thebox.myzen.co.uk/Tutorial/De-coupling.html

Onions' Tiny Board.

Brilliant idea! If only I was more imaginitive...

For version II, include a 0.1 uF across +5 and GND. It seems to help "smooth" analog-to-digital conversions and, in general, it's a good idea...
De-coupling

I notticed the A-DC being wierd, but I had no idea why. The link explains things nicely, so thanks! As well as a 0.1uF capacitor, I thought of having a zener diode from +5V to gnd, regulating the voltage and keeping it at a steady 5V level.

Onions

Thanks for this guys - just what I was looking for.

I am indeed wanting to start with a 'lectric candle project but read 'The List' of supported commands and didn't see random().

So, you can imagine my pleasure at reading here that random() does work! Thanks chaps. Just the job!

[m] :smiley:

Sorry for reopening a old thread. Could this be done with a ATTiny13A chip? I want to exit a random number series as a PWM signal. Doable?

knutolai:
Could this be done with a ATTiny13A chip?

Of course. Just don't exceed the SRAM (random uses four bytes of SRAM) or Flash space. And, in the best case, you only get two PWM pins.

Onions:
As well as a 0.1uF capacitor, I thought of having a zener diode from +5V to gnd, regulating the voltage and keeping it at a steady 5V level.

Onions

A Zener diode will not regulate the voltage - it will clamp the power supply and do all sorts of bad stuff. Don't do it!

The Atmel chips have a maximum working voltage of 5.5V without suffering damage.

I have a setup where a 328P is powered by 6 volts, and nothing is dead a year later, but i would not recommend that you exceed the 5.5V - ever!

// Per.

A series silicon diode and a 1 to 10 uF 10V cap from cathode to ground will convert 6 V to 5.3 V... for about $0.10.
Place the diode, Anode to 6V and Cathode to device Vcc add the capacitor from cathode to ground...
DO NOT forget the capacitor, A diode is a most nonlinear device and the capacitor helps to ameliorate the nonlinearity..
Using that circuit W/O the cap is worse than no diode...
Also a battery will show an increased internal impedance (Ability to supply current is diminished as the battery becomes depleted)
Adding a 47 to 220 uF cap across the battery can increase it's effective life by 50% or more depending on load, capacitor value and use.The capacitor from Cathode to Ground is still required [last thoughts, Doc]

Doc

Docedison:
Adding a 47 to 220 uF cap across the battery can increase it's effective life by 50% or more depending on load, capacitor value and use.

Hmm, head-scratch here.

I can't quite see why adding a capacitor across the battery as well as a capacitor after the diode would be of great advantage over simply putting the larger capacitor after the diode.

Zapro:
A Zener diode will not regulate the voltage - it will clamp the power supply and do all sorts of bad stuff. Don't do it!

Absolutely! Worst of all if you are powering it via a battery of more than 4.5V.