November 22, 2024, 02:16:12 PM

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.


random non-repeating sequence?

Started by ChrisBartram, September 27, 2007, 01:13:21 PM

Previous topic - Next topic

ChrisBartram

Jon;
  I've seen snippets of code like this you've done in the past (though not now that I'm looking for it :-) )... The rest of the coding I'm fine with, but I recall you had an elegant/efficient way of randomly picking through a list of options without repeating.

  In my case each time my prop is triggered I want to send a PLAY Fn  command to my serially attached uMP3, where 'n' is a randomly picked number (ranging 1-8 in my case) that doesn't repeat until it's gone through all the files.

Thanks in advance!

JonnyMac

September 27, 2007, 03:56:27 PM #1 Last Edit: September 27, 2007, 03:59:13 PM by JonnyMac
The key to generating a truly random number with the BASIC Stamp is to call the RANDOM function while you're waiting for something else to happen -- Like this:

Main:
  RANDOM lottery
  IF Trigger = IsOff THEN Main


In this snippet, lottery is a word variable (I use W5 in my Prop-1 programs).  To create a selection that is from 1 to 8, you can do this:

  sfx = lottery // 8 + 1

The modulus ( // ) operator returns the remainder of a division, so it the line above the first section will retun a value from 0 to 7; then by adding 1, you extend the final output from 1 to 8.

You may want to keep track of one more variable, I usually call it 'last', so that you don't repeat the same selection two consecutive cycles:

Select_File:
  RANDOM lottery
  sfx = lottery // 8 + 1
  IF sfx = last THEN Select_File
    last = sfx


Note how we toss another RANDOM call in there so that we can change the value of lottery if sfx is equal to last.  Once we have a difference we can save the new version of last for the next cycle.
Jon McPhalen
EFX-TEK Hollywood Office

ChrisBartram

Got that... But...

I want to cycle through all *8* selections (without repeating any), picking one randomly each time.. i.e. I want to keep track of the ones already played and not repeat them until I've played all 8 selections. :-)

-Chris

JonnyMac

September 27, 2007, 06:42:08 PM #3 Last Edit: September 27, 2007, 06:45:22 PM by JonnyMac
Here's a framework that will get you going.

' =========================================================================
'
'   File...... Play_List.BS1
'   Purpose...
'   Author.... Jon Williams, EFX-TEK
'   E-mail.... jwilliams@efx-tek.com
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


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


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


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

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


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

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


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

SYMBOL  idx             = B2                    ' selection index
SYMBOL  last            = B3                    ' last selection
SYMBOL  mask            = B4                    ' bit mask
SYMBOL  playlist        = B5                    ' "played" selections
SYMBOL  check           = B6                    ' for testing
SYMBOL  lottery         = W5                    ' random value


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

Reset:
  PINS = %00000000

  last = 99


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

Main:
  RANDOM lottery                                ' stir random value
  IF Trigger = IsOff THEN Main                  ' wait for trigger

Select_Option:
  RANDOM lottery                                ' re-stir random #
  idx = idx // 8                                ' select 0 to 7
  IF idx = last THEN Select_Option              ' no repeats
  READ idx, mask
  check = playlist & mask                       ' bit test selection
  IF check > 0 THEN Select_Option               ' already played?
    playlist = playlist | mask                  '  no, update play list
    last = idx                                  '  save idx for next cycle


  ' control code here
  ' -- do something with idx


Check_PlayList:
  IF playlist <> %11111111 THEN Main            ' play  list complete?
    playlist = %00000000                        '   yes, reset
    GOTO Main


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


' -------------------------------------------------------------------------


' -------------------------------------------------------------------------


' -----[ User Data ]-------------------------------------------------------

Bit_Masks:
  EEPROM (%00000001, %00000010, %00000100, %00001000)
  EEPROM (%00010000, %00100000, %01000000, %10000000)
Jon McPhalen
EFX-TEK Hollywood Office

ChrisBartram


ChrisBartram

Quote from: JonnyMac on September 27, 2007, 06:42:08 PM

Select_Option:
  RANDOM lottery                                ' re-stir random #
  idx = idx // 8                                ' select 0 to 7

Jon,
  Shouldn't the line above be:   
  idx = lottery // 8 
?

Was just trying the code out and noticed my random# was always 0 :-)

-Chris

JonnyMac

You're absolutely right, Chris -- sorry about that.
Jon McPhalen
EFX-TEK Hollywood Office

ChrisBartram

Jon --
  Thanks again for all the help. I've been posting videos of some of the props I've created - everything fancy I create is PROP1 controlled!
  A short video of the particular prop that used the random sequence you demonstrated for me here (to pick a random audio 'wise crack' every time it was triggered) has just been posted on YouTube at http://www.youtube.com/watch?v=ezFyERImjRM
  I also posted short videos of all this year's props at http://www.creepynights.org -- photos and videos of the props are in the "Photo Gallery 2007" area.
  There's also a section on projects where how-tos on each are posted (haven't finished all those yet but they're mostly there).

  Looking forward to even creepier and more fun props for next year! Gotta start designing...

  -Creepy Chris
   www.creepynights.org