# Using the randomSeed

June 21, 2000
by Gary Rosenzweig

The point of random numbers is that they are completely unpredictable. This means you can use them to create elements of games that will keep the game play interesting.

So, why would you want to make random numbers predictable? There are two reasons. The first is to create predictable games. Sometimes, these can be more fun than completely random ones. The most famous of all of these would be Pac-Man. In that game, the monsters came after you in a completely predictable way. They followed the same path every time, as long as you followed the same path as well. This led to thousands, perhaps millions, of kids and adults trying to learn the patterns in the game to better their score. Predictability was one of the reasons for its success. You can also create games that have predictable levels of play. As you will see shortly, this is easy to do. The hard part is recognizing when you should make a game random and unpredictable, and when you should make it predictable.

Another reason to make games predictable is to help you debug them. Suppose, for instance, that you have a space ship that moves randomly across the screen, but an error message appears at a certain point in its movement. You use the debug window when you get the error message, but fail to find the problem right away. When you run the movie again, the random movement of the ship takes it all the way across the screen, with no error message. You run the movie many more times, and find that the error does not occur again. This could make it hard to debug. But if you could recreate the original path that the ship followed, then the error will happen in exactly the same spot and you can try different debugging techniques.

Director, like many other programming languages, has a built in random number generator. In Lingo, it is the simple random() function. For instance, random(10) returns a random number from 1 to 10. But computers, which are purely logical, are actually incapable of generating true random numbers. In order to create what seem like random numbers, they use random number generators. A random number generator is a mathematical formula that is so complex that the result seems random and unrelated to the number fed into the formula. You can make a random number generator yourself. Here is a fairly simple one:

r = ((seed*77) mod 10) + 1

When you feed in a 1 as the seed number, you get 8 as an answer. 2 gives you 5 and 3 gives you 2. The numbers seem fairly random. However, with a formula as simple as this, a pattern will soon emerge. The random number generator used by Lingo, although much more complex, also uses a seed number to calculate the random number. Feed the same seed number in and you get the same result. In Lingo, the randomSeed is used to set this otherwise invisible seed number.

```the randomSeed = 1
put random(100)
-- 55
the randomSeed = 2
put random(100)
-- 81
the randomSeed = 3
put random(100)
-- 90
the randomSeed = 1
put random(100)
-- 55
```

Furthermore, the randomSeed is changed after every use of the random function. However, it is changed in such a way that the string of numbers remains the same after the randomSeed is set. Here is a demonstration:

```the randomSeed = 7
put random(100)
-- 71
put random(100)
-- 41
put random(100)
-- 12
the randomSeed = 7
put random(100)
-- 71
put random(100)
-- 41
put random(100)
-- 12
```

Here is a demonstration in Shockwave. Try each of the buttons.

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

By setting the randomSeed to a specific number at the beginning of your movie, the random numbers generated by your movie will be exactly the same. Thus, the random elements of your game will be identical every time you run the game. This will not hold true, however, if your random number generation depends on user input. For instance, if the user can interact with the game elements in such a way as to force them to calculate more random movements or actions, then the same string of random numbers will be used, but at different times. Most of the time, however, this won't be a problem.

Another use for predictable random numbers is to create complex game levels without storing any data or taking up development time. For instance, if you have an adventure game that randomly creates levels of rooms and doors, you might find that the random way the game creates levels does not always produce something very interesting. However, you can use the randomSeed to create exactly the same level again and again. So, as the developer, you try randomSeed numbers. You may then find that 7, 12, 14 and 17 produce useable levels. You then have your program set the randomSeed to one of these numbers to create the level. No data storage is necessary, and the time it took to look at 20 or so levels was much less than the time it may have taken to build 4 levels by hand.

Other uses for the randomSeed include generating predictable patterns with imaging Lingo, creating music, and even creating animated sequences that include lots of moving objects.

Gary Rosenzweig's latest book is "Advanced Lingo for Games." In it, you can find the source code for more than 20 complete games. More information about the book can be found at http://clevermedia.com/resources/bookstore/book4.html. It 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.