Articles Archive
Articles Search
Director Wiki

Random Access

October 3, 1999
by Pat McClellan

Dear Multimedia Handyman,

I am doing a game and need to put sound in. I was wondering if it is possible to have random sound? For example, every time one goes to the next level, one hears a random selection from 6 voiceovers. Thank you.


Dear Jacqui,

Director has a "random()" function which is quite handy in situations like this. Developing in Director, you'll find there are many cases where you need a random "something". I say "something" because it could be a graphic member, a sound member, an integer, a symbol -- almost anything. So what are the options to handle this?

First, let's get clear on how random() works. In the message window, try this:

put random(6)

Chances are, your result was not 2. It could have been 1, 2, 3, 4, 5 or 6. Try it again, several times. You'll find that it returns a random result each time. Note: statistical and computer purists will say that it's not truly random, since it is based on some algorithms somewhere. I've never really bothered to understand their position because for all practical purposes, it's "random enough".

You could simply line up your sound cast members in the first 6 spots of the internal cast. Then, when your player goes up a level (or whenever you want to play a random VO), call this playRandomVO movie script. Note that this is a movie script, not a score script. You don't drop it on a sprite or put it into the script channel. Rather, it will simply reside in your cast. (It will play the sound in channel 1.)

on playRandomVO
  whichChannel = 1
  whichPick = random(6)
  whichSound = member whichPick of castLib 1
  puppetSound whichChannel, whichSound

That's one approach, but not necessarily the most versatile. For example, what if the cast members get moved. Or what if it's not practical -- for whatever reason -- to have the cast members located together. If we call the voiceOver cast members by name, then it really doesn't matter where they are. This approach will require that you maintain a strict naming convention. For example, I'll assume your VO cast members are named "vo1", "vo2", etc.

on playVObyName
  whichChannel = 1
  whichPick = random(6)
  memberName = "vo" & whichPick
  puppetSound whichChannel, memberName

Now, suppose that it's not practical to use the naming convention. Or perhaps you're not sure exactly how many cast members there will be in total, so you don't want to hard code the "random(6)" part. There's another option which is very applicable to many situations; you can simply use a list. In this approach, you'll create a list of the cast names and store the list in a global variable. Since you'll be using a list, you can use the count function to know the parameter for the random() call.

on startMovie
  global gVOlist
  gVOlist = ["youWin", "niceJob", "tooBad", "wayToGo", ¬
    "keepItUp", "youDaMan"]
  -- you can add more sound member names 
  -- without changing anything else
on playVOfromList
  global gVOlist
  whichChannel = 1
  memberName = gVOlist[random(gVOlist.count)]
  puppetSound whichChannel, memberName

The same concept would apply to other situations where you want to randomly select a cast member -- maybe a graphic cast member. For example, what if you wanted to have a sprite change to a randomly selected graphic on rollover?

A sample movie is available for download in Mac or PC format.

You could drop this behavior on the sprite.

property pCastList, pSprite, pNormalMem
on beginSprite me
  pCastList = ["redCircle", "blueSquare", ¬
    "greenTriangle", "yellowOval"]
  pSprite = sprite(me.spriteNum)
  pNormalMem = pSprite.member
end beginSprite
on mouseEnter me
  whichMem = pCastList[random(pCastList.count)]
  pSprite.member = whichMem
end mouseEnter
on mouseLeave me
  pSprite.member = pNormalMem
end mouseLeave

Okay, so now you're catching on to this random() stuff. But you may have noted that random() always returns a positive integer. What if you need to generate a random number that isn't necessarily positive. For example, maybe you've got a game where at various points you need to change the players score, adding or subtracting up to 10 points at random. That means that you'll have a range of -10 up to 10. All we need to do is apply a quick bit of math to our formula.

We have a range of 21 possible values... from -10 to 10, including 0. So we'll need to start by doing a random(21) command, then adjusting the value down to our desired range. We do that simply by subtracting 11.

put random(21) - 11

Just to test the randomness and probability of the random() function, I wrote this quick handler to run a million random samples, then average them. If everything is working well, the average should be darn close to zero. Copy this into a movie script and call it from the message window. You can check the results for yourself.

on runRandom
  y = 0
  repeat with i = 1 to 1000000
    x = random(21) - 11
    y = y + x
  end repeat
  put "average =" && y * 1.0000000000/1000000

There you have several examples of applying the random() function. There are many more ways to use it, which I'm sure you'll discover when you need it. Good luck with your program.

Patrick McClellan is Director Online's co-founder. Pat is Vice President, Managing Director for Jack Morton Worldwide, a global experiential marketing company. He is responsible for the San Francisco office, which helps major technology clients to develop marketing communications programs to reach enterprise and consumer audiences.

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