Post Reply 
Little explorations with HP calculators (no Prime)
04-04-2017, 06:26 PM
Post: #118
RE: Little explorations with the HP calculators
Joe Horn wrote an article (located here) where he discusses a couple of different methods of simulating die casts and shuffling.

I've used a similar technique a couple of times for randomizing a list. The commented code is included below. This is actually pretty much exactly what you describe as your "second idea", but hopefully the comments will help clarify what's going on with it:
Code:
ShufList
\<<
  @ explode the list onto stack and save the size (sz)
  OBJ\-> \-> sz

  \<<
    @ loop: for list item positions 1 to (sz-1), choose a
    @ random item from the remaining candidates and move it
    @ to the target position

    @ note: stack positions (sz...1) are numbered inversely to
    @ list positions (1...sz)

    @ x represents the current target position
    sz 2 FOR x

      @ pick a random item position from the remaining pool
      x RAND * CEIL

      @ move the chosen item to stack level 1
      ROLL

      @ move the chosen item to the current target position
      x ROLLD

    @ update target to the next position
    -1 STEP

    @ implode the resulting list data
    sz \->LIST
  \>>
\>>

The code is reasonably compact and fast. Performance is directly proportional to list size, and on my 50g can handle a list of 100 items in about 1 second. I'm sure someone else here can make a smaller/faster version, but this one has worked well for me.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Little explorations with the HP calculators - DavidM - 04-04-2017 06:26 PM



User(s) browsing this thread: 2 Guest(s)