Random Number Seeds

When I was first learning to write computer programs back in Junior High school (I know, it's hard to believe we actually had computers back then. ) one of the very first things I wanted to do was to write a game program. Doesn't every programmer want to do that at some point?

Anyway, in order to write a game you MUST use a random number generator - what's the fun of a game in which everything happens EXACTLY the same way each time you play? So the action of the game has to have some random component to it.

I remember the first time I used a random function - something like:

I = int(rnd * 10)

I was exceedingly frustrated, because every time I ran the program, it would give me the same numbers each time! What kind of random number generator is that?

After puzzling for a bit, I learned that computers can't really generate random numbers. Instead, they have a particular sequence of numbers that just looks random.

In fact, they have thousands of sequences of numbers. And if you want to get a different sequence of numbers, you have to tell the computer which sequence you want. Like this:

randomize 1 (gives you the first sequence)
randomize 2765 (gives the 2765th sequence)

And if you want to get a different sequence each time you run the program, you have to do something like this:

randomize timer (which picks a sequence based on the time, using the system's clock)

The number you put after the randomize command (1, 2765, timer) is called the random seed

I always thought that was the most pointless thing. Why bother with a "seed"? Why not just automatically randomize timer, instead of making the programmer specify a sequence of "random" numbers?

Turns out, there really is a reason for it, and I actually found a reason this week.

I've been working on a new advertising system for my sites - allowing me to sell advertising space to whoever I want, instead of just relying on Google (if the AdSense program ever goes away, I don't want to be stuck!)

You can see the preliminary version of my new advertising system at the top of this page - there are three advertisements in a horizontal row across the top of the blog.

The ads are placed based on how much the advertiser "bids" for the advertising space. For example, if two advertisers bid 20 cents per click, and two advertisers bid 15 cents, the 20 cent advertisers get the premium spots (top left) and one of the 15 cent advertisers gets the third spot.

But how do you decide which of the 20 cent advertisers gets the very best spot? And how do you decided which of the 15 cent advertisers gets the third spot?

Easy. You use a random number generator. Except...

Each of those advertisements is actually on a separate web page, which means none of the advertisement pages "knows" what ad has been served up by the other advertisement pages. So if I do a randomize each time the page is loaded, it is possible to have the same ad show up more than once (because I get a different random sequence for each advertisement) But I don't want that to happen.

So here is what I do...when this page loads (not the three mini pages, but the actual blog page) this code runs:

Dim seed
randomize timer 'randomize by the sytem clock
seed=int(rnd * 10000) 'pick a random number seed

Now, for each of the three ad pages, when I load them, I pass the seed to them, along with an index variable (1, 2, or 3). And they each run this piece of code:

randomize seed 'randomize based on the seed variable passed in

Now they each load an ad based on the same sequence of numbers. The first page takes the first number in the sequence, the second page takes the second number, the third page takes the third number. Since they are indexing off the same sequence of numbers, this means there are no repeats!

It's nice to know that there really is a good reason for forcing us to pick a random number seed...someone really did know what they were doing!

Posted On Dec 21, 2006 at 5:51 AM    

Previous    Next

 
AddThis Social Bookmark Button

Doug's Random Thoughts

Everything from Soup to Nuts,
And Maybe Even More!


ABOUT ME

Current Posts

Blog Is Moving...
Emergency Room
Last Entry
Join The Chorus
Jeorge Sings A Hymn




My Links

Educational Software Solutions
My Playground
My Favorite Puzzle

Next Entries

Merry Christmas
Comment Spam
One, Two, Three
Website Update
Word Game, Math Game

Previous Entries

Adders!
A Random Assortment
Building Puzzles - Tile Puzzler
Tile Puzzler
Playing and Recording the Bass Guitar

Index and Search

Archive
Blog Search

Bible Index
Bible Search

Blog designed by Virtu Software