Articles Archive
Articles Search
Director Wiki
 

Creating a Random List

October 11, 2000
by Gary Rosenzweig

Random elements are great additions to any Director movie. Let's say, for example, that you have a set of ten sounds, one of which is selected at random to play when a user clicks a button. The problem with this type of randomness is that it is very possible for the same sound to be played twice in a row. A simple solution might be to store the name or number of the last sound played, and make sure that the next randomly selected sound is not the same as the previous one. A handler to do that might look something like this:

global gPreviousSound

on playRandomSound

  repeat while TRUE
    r = random(10)
    if r <> gPreviousSound then exit repeat
  end repeat

  gPreviousSound = r
  puppetSound 1, "sound"&&r

end

In this case, a repeat loop is used to pick the random number over and over again until a different number than the previous one is selected. It is a simple technique that gets the job done. However, it is still possible to get odd sequences, such as the same two sounds played over and over again. Or, more likely, a sound or two will not be played at all, just by chance.

A better way to insure that all sounds are played in the most random-seeming manner is to create a shuffled list. This is a list containing the same elements as the original, but sorted completely randomly. The elements are the same, but the order is random.

Using a shuffled list, you can play each sound exactly once. Once all the sounds are done, you have to start at the beginning of the list again, or shuffle again. Both of these options have problems. Playing the list again means that the sounds now have a predictable order, as same sequence of 10 sounds will repeat over and over. Shuffling again means that you risk the chance of having the same sound play twice in a row. This would happen if the first shuffle placed the sound 10th, and the second shuffle placed it first.

A good way to avoid this problem is to reshuffle the list in halves. All of the sounds randomly placed in the first half of the list will remain in the first half of the list, but in a new order. The same goes for the second half of the list. Here is a handle that does this reshuffle.

on reshuffle list

  newlist = []

  -- get halfway mark
  m = list.count/2

  -- randomize first half of list
  i = 1
  repeat while m > 0
    r = random(m)
    newlist.add(list[r])
    list.deleteAt(r)
    m = m - 1
  end repeat

  -- randomize rest of list
  repeat while list.count > 0
    r = random(list.count)
    newlist.add(list[r])
    list.deleteAt(r)
  end repeat

  return newlist

end

An example would be to sort a list of numbers. First, an ordered list is created. Then, the list is shuffled. On a reshuffle, the numbers get a new order, but the two halves of the list remain the same.

on test

  myList = [1,2,3,4,5,6,7,8,9,10]
  myList = shuffle(myList)
  put myList

  myList = reshuffle(myList)
  put myList

end

The result in the message window might look like this:

test
-- [8, 4, 9, 1, 2, 6, 7, 5, 3, 10]
-- [2, 9, 1, 8, 4, 5, 6, 10, 3, 7]

Notice that the numbers 1, 2, 4, 8 and 9 appear in the first half of the list, even after the reshuffle. This insures that the same sound never plays twice in a row, and that a pattern never appears. Adding this sort of quality to your random lists can really help to make a Director movie seem more professional.

Gary Rosenzweig's two most recent books are: "Special Edition Using Director 8" -- The most comprehensive guide to Director ever, including tons of examples and demo movies. It's suitable for novices and experts alike. "Advanced Lingo for Games" -- How to make games with Director 7 and 8. This book comes complete with full source code for more than 20 complete games. More information about these books can be found at http://clevermedia.com/resources/bookstore/. They can be purchased there, or in your local bookstore.

Gary Rosenzweig is the Chief Engineer, founder, and owner of CleverMedia, a game and multimedia development company in Denver, Colorado. He is the author of ten books on Macromedia Director and Flash, including his latest, Special Edition Using Director MX.

Copyright 1997-2017, Director Online. Article content copyright by respective authors.