Random number solution
Hi all, I came up with a simple way to generate random numbers in hardware for the SX, Basic Stamp, or Propeller.
All you need is one resistor, one capacitor, and one pin from the device.
1. Set up the resistor and capacitor in parallel between the pin and ground.
2. Set the pin to output and high voltage.
3. Set the pin as an input and count the clock cycles it takes for the voltage to reach the logical transition.
4. Use the least significant bit of the count as a coin flip.
Once you have random coin flips you can make any random number, for example 8 in a row give a random # from 0 to 255.
For the basic Stamp the RCTIME command will work, or for the SX you can enable the RTCC.
The randomness comes from the noise inherent in the circuit.
As long as the RCTIME is long compared to the clock speed, the logical transition will be brought about by a noise induced fluctuation.
It is best to use TTL logic level for this because the slope of the exponential decay is flatter towards lower voltages, which makes it more likely that a random voltage fluctuation will bring about the transition.
This works quite well, and the only disadvantage I can see is that you need to wait many clock cycles per random bit.
One solution would be to have it run in the background as an interrupt.
You could run it as a debouncing method that also generates random numbers. One millisecond wait after a button push would effectively debounce the contact and give you time to generate digits.
The random bits could also be used to seed a psuedo random generator.
In any case it's cheap, easy, and it works for me.![smile.gif](http://forums.parallax.com/images/smilies/smile.gif)
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I wonder if this wire is hot...
All you need is one resistor, one capacitor, and one pin from the device.
1. Set up the resistor and capacitor in parallel between the pin and ground.
2. Set the pin to output and high voltage.
3. Set the pin as an input and count the clock cycles it takes for the voltage to reach the logical transition.
4. Use the least significant bit of the count as a coin flip.
Once you have random coin flips you can make any random number, for example 8 in a row give a random # from 0 to 255.
For the basic Stamp the RCTIME command will work, or for the SX you can enable the RTCC.
The randomness comes from the noise inherent in the circuit.
As long as the RCTIME is long compared to the clock speed, the logical transition will be brought about by a noise induced fluctuation.
It is best to use TTL logic level for this because the slope of the exponential decay is flatter towards lower voltages, which makes it more likely that a random voltage fluctuation will bring about the transition.
This works quite well, and the only disadvantage I can see is that you need to wait many clock cycles per random bit.
One solution would be to have it run in the background as an interrupt.
You could run it as a debouncing method that also generates random numbers. One millisecond wait after a button push would effectively debounce the contact and give you time to generate digits.
The random bits could also be used to seed a psuedo random generator.
In any case it's cheap, easy, and it works for me.
![smile.gif](http://forums.parallax.com/images/smilies/smile.gif)
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I wonder if this wire is hot...
Comments
They also repeat at a maximum interval equal to the highest available value in the numeric register.
What I described above is a way to generate random seeds in hardware so the sequence of random numbers is not always the same every time you power up the device. If you have a human pushing buttons it is generally not a problem since you can use the value of the Real Time Clock Counter (RTCC) either as is, or to seed the generator.
I am finishing up a necklace for my wife that has randomly blinking LED's, and because of the nature of my insanity I want them to blink randomly, instead of according to a pattern. The LED's also get brighter and dimmer because the rate of flickering is also randomized. It has a nice little circuit board on the back with an SX20 running off a single CR2430 battery. When it's done I will post some pics.
What is your project JonnyMac?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I wonder if this wire is hot...
I also don't understand the first line LFSR: 'randomize "seed"
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I wonder if this wire is hot...
LFSR: is just a label; LFSR stands for Linear Feedback Shift Register.
The program in question will appear in the March issue of Nuts & Volts. It's a mini game board that uses an 8x8 LED array as the screen, and has four push-buttons for controls. The demo program is an embedded version of Conway's Game of Life. One of the setup options is to randomize the colony, hence the need for the code above.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I wonder if this wire is hot...
Part of the reason I wrote this demo program is to show how to use an 8-byte array as a 64-bit array, with custom functions in the program like GET_BIT, SET_BIT, and CLR_BIT.
Why not just enable the RTCC to increment every clock cycle and use whatever value it is at when the button is pushed to seed the generator?
Even at 1 MHz it will roll over 4000 times per second.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I wonder if this wire is hot...
Your RTCC value idea is a novel one for programs that are not enabling periodic interrupts.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I wonder if this wire is hot...