[Circuitsville logo]
[ Home ] [ Services ] [ Anadigm ] Micro Controllers [ Contact ] [ About ] [ Links ] [ Atmel Microcontroller & Arduino Hardware  ] [ Applications for Arduino - Real Time Clock ] [ Applications for Arduino - Pseudo Random Numbers ] [ Applications for Arduino - Extended Input/Output ] [ Arduino Shield Starter Templates ] [ ] [ ]

        
           
		

Pseudo Random Number Generator

This started in church, of all places. Many months ago I had replaced the "eternal flame" candle with a battery powered fake candle to save on maintenance. Unfortunately climbing the ladder every third week to change the batteries was still more maintenance then desired. So I came up with a battery eliminator driven by a wall wart and now there was no maintenance needed.

But sitting in the pew that morning, the thing sure wasn't behaving like a candle. The "flame" was too constant, no flickering, no guttering in the breeze. In other words not random enough.

But how to fix that?

During some of my continuing education I had taken courses in State Machine Design. One thing that had been discussed was the Pseudo Random Counter, also known as a Pseudo Random Number Generator. A PRC generates a sequence that appears random but repeats after a certain number of values. For example an eight bit sequence could be (in hexidecimal) 0xF0, 0xE1, 0xC2, 0x85 and 250 other combinations before repeating. If each output bit drove an LED this would appear to blink randomly, becoming lighter and dimmer just as a candle would do.

To do this you use an N element shift register with some logic gates to pick off certain values and feed them back into the least significant bit or LSB. The Arduino Nano uses 8 bit registers, so that could give me up to a 256 count. (For mathematical rigor, N = 8) Most often the logic gates are exclusive OR (or XOR) gates, how could I do that in software? How would I know where to pick off for the feedback?

Fortunately during my web research I found a paper while searching the web which had an 8 bit (full byte) shift register implementation. It was from a course at Virginia Tech taught by Professor Patrick Schaumont, page 23 had the key diagram. Admittedly, If I had read about two more pages my project would have been simpler, but .... I didn't.

This is the schematic of the logic: [ 8 Bit Pseudo Random Circuit ]

This is all well and good but Arduino does NOT have an Exclusive OR (a.k.a. XOR) function in the logic. (Or I couldn't find it when I was developing the code, there might be a bit-wise XOR now.) So how do I do that?

The definition of the OR function is that a true out when either or both inputs gives a true out. The exclusive OR or XOR function give a true out when either inputs are true but is not true if both inputs are true. Here is a truth table to help explain this:
[ Exclusive OR Truth Table ]

Understanding Boolean algebra the XOR function is:

= (A * ~B) + (~A * B)


which is spoken as

A and not B OR not A and B


This and the truth table led to the IF function:
                    if(((eReg= =1)&&(cReg= =0))||((eReg= =0)&&(cReg= =1))){

fReg = 0x01;
}
else{
fReg = 0x00;
}

(&& is logical AND and || is logical OR in Arduino code.)



Code to Download is here (for Arduino 18) A print of the code as PDF is here

It took a couple of tries, but finally I had along sequence (255 steps) that appeared random but repeated. The state 0x00 (in hexadecimal) or b0000 0000 is the short state, it stays stuck as all zeros. But that is fine because you don't want the candle to blow out. Now I could go build my candle.




Back to top
Contact Us
©Copyright 2006 - 2016 Circuitsville Engineering,
revised 25 January 2016