Shop OBEX P1 Docs P2 Docs Learn Events
Random number solution — Parallax Forums

Random number solution

ElectronegativityElectronegativity Posts: 311
edited 2007-01-24 20:56 in General Discussion
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

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I wonder if this wire is hot...

Comments

  • JonnyMacJonnyMac Posts: 8,943
    edited 2007-01-23 23:39
    I needed a running, 8-bit random number for a program I jsut wrote so I stuck this little chunk of code (adapted from www.sxlist.com) into my ISR. It works really well and I just copy "seed" when I need a random value.

    LFSR:                        ' randomize "seed"
      IF seed = 0 THEN
        seed = 24
      ENDIF
      ASM
        MOV  W, #$1D
        CLRB C
        RL   seed
        SNB  C
        XOR  seed, W
      ENDASM
    
    
  • ElectronegativityElectronegativity Posts: 311
    edited 2007-01-23 23:57
    Those generators are nice, but always generate the same sequence of numbers if you start with the same seed.
    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...
  • JonnyMacJonnyMac Posts: 8,943
    edited 2007-01-24 00:15
    You're right about the sequence generation, which is why I stuck it into the ISR -- that way it's always changing. I'm not suggesting it's better or worse, just another way to get around the "limitations" of the standard RANDOM functions (in my case when I have an ISR, anyway).
  • ElectronegativityElectronegativity Posts: 311
    edited 2007-01-24 00:37
    How does putting that code in the ISR make it constantly change?

    I also don't understand the first line LFSR: 'randomize "seed"

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    I wonder if this wire is hot...
  • JonnyMacJonnyMac Posts: 8,943
    edited 2007-01-24 04:43
    Since the ISR is always running (in the "background") that code gets executed every time through, so "seed" changes with every interrupt call (1000 times per second in my program). When I need a random value I just copy "seed" into another variable.

    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.
  • ElectronegativityElectronegativity Posts: 311
    edited 2007-01-24 16:32
    When you have a power on reset does the game always start with the same configuration?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    I wonder if this wire is hot...
  • JonnyMacJonnyMac Posts: 8,943
    edited 2007-01-24 18:02
    After a scrolling start-up message the game displays a ? prompt. Since the display is controlled by the ISR and that is running every millisecond, the random number (seed) is constantly changed. When you press the Randomize button at the ? prompt you get a randomly filled colony; hold it down and the screen is constantly randomized until you let go. Once you press the Run button the game rules are put into play. The other two buttons let you scroll through pre-set patterns that are stored in DATA tables.

    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.
  • ElectronegativityElectronegativity Posts: 311
    edited 2007-01-24 20:14
    Sounds like a good project, but you really don't need to run the ISR at all.

    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...
  • JonnyMacJonnyMac Posts: 8,943
    edited 2007-01-24 20:43
    The project uses an 8x8 LED matrix that is multiplexed, hence the use of the ISR (yes, I could have done without it, but then I would have need to pad the code to get fixed-rate display refreshing -- the ISR was a better call IMO). As the ISR was in place, I made the decision to put the LFSR code into it; for other programs I would just create a standard function. Since I do in fact have an ISR, and it is conveniently running once each millisecond, I also update a timer variable that can be used for creating known delays (as PAUSE is adversely affected by the ISR). And my ISR does not run the same number of cycles on every pass so I am not able to use the EffectiveHertz option of FREQ which would have enabled the use of PAUSE.

    Your RTCC value idea is a novel one for programs that are not enabling periodic interrupts.
  • ElectronegativityElectronegativity Posts: 311
    edited 2007-01-24 20:56
    I will be looking forward to seeing it in Nuts and Volts.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    I wonder if this wire is hot...
Sign In or Register to comment.