November 22, 2024, 07:49:57 AM

News:

Got VSA?  Want to use your Prop-SX?  Now you can!  See the VSA section of the Library forum for Prop-SX code that works with VSA.


A question on random numbers

Started by GOT, August 11, 2008, 07:05:03 PM

Previous topic - Next topic

GOT

I was playing with random numbers and was getting strange results.  I didn't care if I got the same random numbers everytime I ran the program, but it looked like I was getting about 10 -15 random numbers cycling over and over within the same program.

Here is my program to make the servo move to a random place at a random speed.  All variables are WORDs.

pos             VAR     WORD                    ' servo position
xnum         VAR      WORD
ynum         VAR      WORD
stp         VAR      WORD


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

Setup:
  LOW Servo                                     ' make P0 output and low


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

Main:

pos = 75
DO

RANDOM ynum
RANDOM stp

ynum = ynum//(400)
   
stp = (ynum // 11) + 1
xnum = pos

FOR pos = xnum TO ynum STEP stp               
     PULSOUT Servo, pos +450                     
     PAUSE 18
NEXT

LOOP        ' back to Main

The speed (stp) never seemed to change and I get the same 14-15 movements over and over again.

JonnyMac

August 12, 2008, 08:45:56 AM #1 Last Edit: August 12, 2008, 08:56:03 AM by JonnyMac
Remember, RANDOM isn't truely random, it's psuedo-random.  What this means is that RANDOM is actually a mathematic function (called a linear feedback shift register) that takes its input value (called the seed) and spits out a new value that kind of looks random.  The fact is, however, same seed in, same result out (this can be useful in some applications, such as a Simon game).

The key to using RANDOM effectively is allowing it to run but not be used until some external event allows it -- a trigger for your prop, for example.  You'll often see this in my programs:

Main:
  timer = 0
  DO WHILE timer < 100
    RANDOM lottery
    PAUSE 5
    timer = timer + 5 * Trigger
  LOOP


This will tumble the "random" value until an external trigger allows the value to fall through.  If you can't do this then what you might want to do is seed RANDOM in your setup section with a value that will give you the kind of results you're looking for.

Let me suggest that you add a word variable called lottery and code your program like this -- see if this works better:

Setup:
  LOW Servo                                     ' make P0 output and low
  lottery = 1031                                ' seed RANDOM
  pos = 75                                      ' starting position

Main:
  RANDOM lottery
  ynum = lottery // 400
  RANDOM lottery
  stp = lottery // 11 + 1
  xnum = pos

  DEBUG DEC xnum, TAB, DEC ynum, TAB, DEC stp, CR

  FOR pos = xnum TO ynum STEP stp
    PULSOUT Servo, pos + 450
    PAUSE 18
    RANDOM lottery                              ' tumble random value
  NEXT

  GOTO Main
Jon McPhalen
EFX-TEK Hollywood Office

GOT

I am aware of seed values and don't care if my seed is the same each time.  The issue I saw was that it seemed like I was only getting about 15 "random" numbers until they started to repeat.  Was this because my variable values were initially undefined?

JonnyMac

Yes, and because you're running the output of RANDOM through the same code.  By separating your random value (I always call it lottery in my programs) it can use the full 16-bit range, and values derived from it will appear more random.  Also, stirring the random value more frequently will help.  I used your servo loop as this doesn't run the same number of iterations each time through so, again, the output should appear more random.
Jon McPhalen
EFX-TEK Hollywood Office