November 21, 2024, 11:34:59 AM

News:

You can now use Vixen to program your Prop-1 and Prop-2 controllers!  Get started quickly and easily, without having to learn PBASIC.  Details in the Library forum.


RANDOM lottery

Started by JackMan, September 25, 2010, 05:11:10 AM

Previous topic - Next topic

JackMan

In the basic "Check_Trigger" code I've noticed that sometimes the line "RANDOM lottery" is included. What does this do that makes it different from when this line is not used?

JonnyMac

September 25, 2010, 08:08:09 AM #1 Last Edit: September 25, 2010, 08:10:53 AM by JonnyMac
RANDOM takes its seed value (lottery) and runs it through an algorithm called an LFSR (linear feedback shift register) to scramble the value into an apparently random number.  It really isn't; same seed in, same result out -- but you won't get a repeated until the entire series (65536 values) has run.

What we need to do is introduce some external randomness -- the trigger event (since we don't know when that will happen).  If we're constantly changing the random seed while waiting on a trigger it will be random when the even happens.  

Here's a way to prove that RANDOM truly isn't (unless stirred up as we do).  This little program will light an LED on the Trainer when the button is pressed.  Run the program and write down the order of lights.  Now press the Reset button on the Prop-1 to restart the program (which resets lottery to 0).  Note how the order -- while it looks kind of random -- is the same as last time.  This is the result of the LFSR giving the same result for the same seed.  

In fact, you'll get the same results as I do: 2, 0, 1, 3, 2, 4, 4...

After you see this, move the RANDOM lottery line into the trigger loop and run the program again.

' =========================================================================
'
'   File......
'   Purpose...
'   Author....
'   E-mail....
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------


' -----[ Revision History ]------------------------------------------------


' -----[ I/O Definitions ]-------------------------------------------------

SYMBOL  Sio             = 7                     ' SETUP = UP; no ULN
SYMBOL  Trigger         = PIN6                  ' SETUP = DN


' -----[ Constants ]-------------------------------------------------------

SYMBOL  IsOn            = 1                     ' for active-high in/out
SYMBOL  IsOff           = 0


' -----[ Variables ]-------------------------------------------------------

SYMBOL  timer           = B2
SYMBOL  thePin          = B3

SYMBOL  lottery         = W5


' -----[ Initialization ]--------------------------------------------------

Reset:
 PINS = %00000000                              ' clear all outputs
 DIRS = %00111111                              ' make P0-P5 outputs


' -----[ Program Code ]----------------------------------------------------

Main:
 timer = 0                                     ' reset timer

Check_Trigger:
 PAUSE 5                                       ' loop pad
 timer = timer + 5 * Trigger                   ' update timer
 IF timer < 100 THEN Check_Trigger             ' wait for 0.1 sec input

 RANDOM lottery                                ' stir outside loop

 thePin = lottery // 5                         ' for P0 to P5
 HIGH thePin                                   ' light the pin
 PAUSE 2000                                    ' wait a bit
 PINS = IsOff                                  ' clear all

 GOTO Main


' -----[ Subroutines ]-----------------------------------------------------


' -----[ User Data ]-------------------------------------------------------
Jon McPhalen
EFX-TEK Hollywood Office