Categories
Geek / Technical

Interesting Results from Testing Random Number Generation Assertions

Taking Ken’s advice from my previous post, I decided to write a small test program to verify that the RNG method touted as the best really was the best.

To recap, there are three methods:
The first method involves using the modulus operator: int pickedNum = rand() % range;
The better method involves some trickery to get past the low-order bits problem: int pickedNum = rand() / (RAND_MAX / range + 1 )
And the best method involves a loop discarding the values that are not within the range:

int randDivisor = RAND_MAX / MAX_RANGE;
unsigned int numPicked;
do
{
numPicked = rand() / randDivisor;
}
while (numPicked >= MAX_RANGE);

I wrote up some code to seed the C++ random number generator, run through the loop for a certain sample size for each method, and then output the results. I was expecting that the best method described in the article would show a better, more even distribution. My test checked the distribution over a range of numbers. I had a vector that stored the number of times a value within that range was picked. That is, if the RNG method output a value of 3, I would increment the value in the vector associated with 3.

I also stored the start time and end time for each loop. Besides accuracy, I think speed would be important for game developers.

I ran it with different sample sizes and different seeds, and each time I seem to come to the same conclusion.

The supposedly “best” method was actually slightly worse than the “better” method.

How did I define what was good and what was bad? I already think that the method described in the article was simpler to write and understand, which means that it wins when it comes to readability and maintainability. Of course, this code will likely be written once and left in some utility class, and the other methods are fairly easy to understand, so we’ll look at other criteria.

How flat is the distribution? If an RNG picks one number much more often than the rest, then it wouldn’t be very good. We would ideally like something that was truly random, so the method that gives us the distribution that was closest to random would be best. Now, I am no expert on statistics, but my check was to subtract the maximum number of times a value was picked from the minimum number of times a value was picked. For an ideal distribution, the answer would be 0. If one distribution’s min to max range is closer to 0 than another, then we can probably assume that the method used to create that first distribution is closer to truly random.

How long does it take to compute a result? If a method produces superior results but takes multiple seconds to calculate each time, it won’t really be useful in a video game which generally tries to calculate results within milliseconds.

So those were my two criteria. If you are very familiar with statistics, perhaps you have already seen a flaw with my test. Please let me know because I would rather have the truth than be correct.

Here are the results when run over a range of 10000 numbers and a sample size of 100000000, using a seed of time(NULL) (the same value was used for each method, so the seed value was created at the start of the program):


Time to finish first method: 9
Time to finish better method: 8
Time to finish best method: 12


Diff between min and max values:
First: 10398 - 9641 = 757
Better: 10414 - 9626 = 788
Best: 10416 - 9621 = 795

I ran it multiple times, and the Best method seemed to always have a large range between the minimum and the maximum number of times a value was picked. It also consistently ran 2 or 3 seconds slower than the other methods.

And using a seed of 0:


Time to finish first method: 9
Time to finish better method: 8
Time to finish best method: 11


Diff between min and max values
First: 10445 - 9629 = 816
Better: 10409 - 9586 = 823
Best: 10395 - 9621 = 774

I ran the test using a much smaller range of values. Instead of 10,000, I used 10.


Time to finish first method: 9
Time to finish better method: 8
Time to finish best method: 10


Diff between min and max values
First: 10006453 - 9992621 = 13832
Better: 10001752 - 9998124 = 3628
Best: 10001752 - 9998124 = 3628

Over a smaller range of values, it seems that the Best and Better methods have the same difference between the minimum and maximum number of times a value was picked. Best was still consistently slower, though.

So what do these results mean?

The trick of dividing by RAND_MAX uses the high order bits of a random number produced by rand(), thus alleviating the old problem of poor rand() implementations with non-random low order bits. But as I said, these days that problem has been largely fixed by library vendors, so the only thing you have managed to do is make the code more complicated. It buys you nothing, and before you start thinking that doing the same thing with floating-point will help, I assure you that it will not.

Well, it seems that according to my tests it buys time, but then the article does mention using a uniform deviate from the random number. I then added that version to my test, and recomputed the results.

For a small range, I found that it ran at the same speed as the first two methods while also providing the same difference between minimum and maximum number of times a value is picked as the Better or Best method.

For a very large range (100000 values), however, I got the following results:

Time to finish first method: 11
Time to finish better method: 11
Time to finish best method: 13
Time to finish deviate method: 11


Diff between min and max values
First: 1178 - 860 = 318
Better: 1140 - 238 = 902
Best: 1135 - 861 = 274
Deviate: 1142 - 865 = 277

As you can see, the uniform deviate method was computed at about the same speed as the non-looping methods. While being only slightly worse at flattening the distribution as compared to the Best method, it blew away the Better method.

However, I also would like to note that the original method, which was supposedly so bad, seems to have a fairly flat distribution, too. Of course, once you get to a smaller range of values, it got much, much worse. If you only checked a range of 10 values, the first method came back with over 3 times the difference of the other methods, and the deviate method worked about as well as the Best, coming back with the same flatness in distribution.

Apparently which method you use depends on what range of values you are checking. If you are checking a small range of values, then you would do well NOT to use the First method. If you are checking a large range of values, it doesn’t seem to matter as much. In any case, the “Best” method is not named right, but the uniform deviate does correct its speed problem.

Potential Problems with These Tests:
Subtracting the minimum number of times a value is picked from the minimum number of times a value is picked might not really give me a meaningful value like I think it does. If you can think of a better way to handle this test, please feel free to correct me.

If you would like the C++ source, you can download it:
randomtest.tar.gz
randomtest.zip

Categories
Game Development Geek / Technical

The Best Random Number Generation Explanation Ever

Thanks go to Uhfgood in #gamedevelopers for finding the article Using rand(). I thought I already knew about the problems with using rand(). This article was great at clarifying the actual problems people have with using rand() as well as faults with “good” solutions people use. It also documents a simpler way to use rand() that actually produces better results! It turns out that rand() is better than many programmers thought.

For those of you who don’t know, the classic way people are taught to use rand() to get a random number between 0 and maxRange is as follows:


int picked = rand() % maxRange;

The problem with the above is that the value you get in picked will not be as random as it could be. Something about lower order bits repeating too often. So usually the “better” way to solve the problem is through some slightly more complicated code:


int picked = lowerBound + rand() / ( RAND_MAX / ( maxRange - lowerBound ) + 1 );

The article explains that this example also has problems!

So what do you do? Stop fighting rand()! I wasn’t happy with the example given as I don’t understand why simple variables have to be used with simple examples since they only make it more confusing. The following is my own code:


int highest = 100;
int randDivisor = RAND_MAX / highest;
int pickedValue;
do
{
pickedValue = rand() / randDivisor;
}
while ( pickedValue >= highest );

It may seem like you would waste too many cycles waiting for pickedValue to be within the range you desire, but it really doesn’t take long at all. It is more pseudorandom than the other examples and needs less complex code, too!

The document also explains the problem with the usual code to seed the random number generator:


srand(time(NULL));

The problem seems to come from the fact that changes from one time to another aren’t usually enough to change the first randomly generated number. The article explains a way to use time() in a portable manner by hashing the bytes of its return value.

The entire article is a good read, and I believe any game programmer will find it useful.

Categories
Game Development Games Geek / Technical Linux Game Development

Why Flash 9 for Linux Is Taking So Long

Paul Betlem, senior director of engineering for Adobe, explained why Flash 9 for Linux is taking so long.

GNU/Linux users didn’t even see Flash v8, which meant that while Windows and Mac OS X users were able to use and view newer content, GNU/Linux users had to deal with a wide range of problems due to an outdated plugin.

The problem was that Adobe wanted to create a consistent experience for all distributions, and the Linux Standards Base has not addressed all of the different libraries used by Flash. Testing multiple configurations was also a challenge. The good news is that Adobe’s suggestions to the LSB aren’t falling on deaf ears, and it should be easier in the future to provide an application that can run on any distro without the user or developer worrying about tiny but important differences.

Also good news is that Adobe plans to ship Flash v10 for Windows, Mac, and Linux-based systems simultaneously, so the delay GNU/Linux users had seen with v9 apparently won’t happen again.

So what does it mean to GNU/Linux gamers? Flash games will no longer be off-limits. And for developers, it means an entirely new audience can be available to play their games.

Categories
Geek / Technical Marketing/Business

Top 10 Geek Business Myths

Last month, Ron Garrett of Rondam Ramblings posted an interesting article called Top Ten Geek Business Myths.

Many new entrepreneurs fail because they focus on the wrong things. Filing for patents to protect an idea and getting millions in startup capital won’t help you one bit if you forget to focus on getting sales, and getting sales means you need to focus on the customer. The customer has needs, thoughts, and concerns. Address them, and you’ll be fine.

The bonus 11th myth, “After the IPO I’ll be happy”, addresses a fallacy that isn’t specific to entrepreneurs. I’d like to generalize it to the idea that happiness is “out there”. If you connect your happiness to some accomplishment or goal, you are basically saying that you won’t be happy unless you succeed. What happens if you don’t succeed? What if you change your goals? You should BE happy doing whatever you are doing. You shouldn’t become happy only once you finish. It otherwise sounds too much like work. If you don’t enjoy it, why are you doing it?

Categories
Geek / Technical General Politics/Government

How Well Do You Know GNU?

Recently the Free Software Foundation put forth a call for volunteers to help answer licensing questions about the GPL, LGPL, and Free Software licenses in general.

The Compliance Lab does do one job which is very public: we answer licensing questions from the free software community. When people want to learn how they can mix code under different terms, or what license would be best for their program, we try to help them so they can spend less time worrying about legal nitty-gritty and more time hacking. It’s not the most glamorous job, but it’s a unique way to help out.

If you are interested in volunteering, or even if you just want to see how well you understand the GPL, take the Free Software licensing quiz.

The questions can be tricky, especially if you are not at all familiar with the GPL or LGPL. Some things might surprise you. I answered four or five answers correctly, which shows that I have a few things to learn. Even if you don’t want to volunteer, the quiz is informative, so go ahead and see how well you do.

Categories
Game Development Geek / Technical

Automating Build and Test Systems

Years ago, I read Automating the Build Process at Gamasutra, which documented an automated build process for the game Creatures 3. The advantages for implementing an automated build process include better reliability, reduced time, and reduced risk.

A few weeks ago, a new article has appeared called
Automated Build and Test Systems for Games, which outlines what Nihilistic Software does when developing their own games. Once again, time savings are emphasized.

In both articles, it seemed that some customization was needed, but you should be able to find a way to automate the process for your own games. One tool I found is BuildBot, which mentions among its users id, which uses it for Return to Castle Wolfenstein: Enemy Territory.

The overall goal is to reduce tree breakage and provide a platform to run tests or code-quality checks that are too annoying or pedantic for any human to waste their time with. Developers get immediate (and potentially public) feedback about their changes, encouraging them to be more careful about testing before checkin.

Any time you can use a computer to automate a repetitive task, you’ll find consistency in quality and speed as well as fewer headaches related to the meta-work of making a game. While I believe that having it automatically build everytime a change is made would be overkill for a one-indie shop, having it delegated to a button-press would definitely help.

Categories
Games Geek / Technical General

Are Games Art Chat Log Available at Manifesto Games

Sometime back Gamasutra posted a news item regardinga discussion about games as art held at Manifesto Games.

Are games art? If not, why not? And if so, why? Is thinking of games as art useful or actually a hindrance for game developers? If games are art, what should our aspirations for the form be?

MIT’s Henry Jenkins, video game theory professor Jesper Juul, game designer Santiago Siri and gameLab’s Eric Zimmerman were invited to argue whether or not games could be considered art and who gets to define it as such. It was a playful discussion, with comments ranging from the humorous to the serious. While nothing definitive was decided, it did show that labeling video games as a form of art is difficult, but it is not because they are inherently not artistic, as Roger Ebert would claim.

The complete chat log can be found at Manifesto Games.

Categories
Games Geek / Technical Marketing/Business

I Played with a Wii

While out buying some much needed replacement gym shoes (I haven’t rotated them every 100,000 steps like I should have), I decided to stop into a Game Stop and try out the Wii on display. I had heard that they require a credit card, but it turns out that they only needed a state ID.

Ok, seriously, why would you purposefully put up barriers to get your potential customers to try out your products? “Come check out our cool stuff that we want you to purchase! It’s amazing! It’s sweet! You’ll love it! Not so fast there, Mr. Customer! First you need to give us something. We can’t just trust you to try it out, after all.”

When I was given the Wii remote, it was in a steering wheel. Apparently Ubisoft makes plastic steering wheel-shaped holders for the remote? The reason for the wheel was to let it feel more natural while playing Excite Truck. It was the only game on display, which disappointed me. I was looking forward to some Wii-sports or something other than a game in the only genre that I don’t care much about.

I’m not a racing fan. I will play Cruisin’ USA or similar arcade games because for some reason out of all of the games that my female friends could play, racing games appeal the most. And not games like Super Mario Kart. No, they like realistic racers with no missiles to shoot or banana peels to drop or oil slicks to leave behind for the next player. I’m sure it appeals to a lot of people. It just doesn’t appeal to me.

Anyway, I start working with the menu, and I wasn’t sure if the remote wasn’t calibrated or if my hand was shaking (I had been playing basketball not too long before), but eventually I got used to it. It was kind of cool to have the remote rumble when it highlights something that can be picked. I checked out a few of the options, such as the photo album and the calendar. I even left a note, “gbgames was here”. Childish? Maybe. What’s it to ya’?

So I finally start the game. It had a tutorial section, and I made it through a good portion of it with only a little difficulty. Part of it was getting used to the controls. My video-game-playing hands kept hitting the directional controls whenever I wanted to turn. At first, I kept forgetting that you need to turn the controller itself. Eventually I got the hang of it, but I swear it reminded me of my Atari 2600 days. I would dodge, jump, and dive with that controller because it still wasn’t obvious to me that doing so was a waste of energy. With the Wii, it actually becomes not only functional, but necessary.

I did a bunch of tricks and jumps, but I could not get the 720 air spin down. I eventually quit the tutorial and started a race. Apparently while racing three laps I also had to earn a certain number of stars. It was interesting, but just like in most racing games, I found myself struggling to stay on the track. I got a lot of air, though, and a few times I managed to spin 360 degrees and land perfectly to get a speed boost. All the while I was spinning, tilting, and turning the controller.

In my limited period of time playing with the Wii, I thought it was promising. I just wish that the demo had something more interesting for me to try out. This weekend, plenty of people will receive their preorders for the Wii and probably the new Zelda game. I will not be among those people, but that just means that when I do eventually get a Wii, it will have more games and the price will probably have come down a bit. Oh, and maybe I will have more time to play games.

I will eventually get a Wii…once I have something more compelling than Excite Truck to play. When the N64 was in kiosks, I had a blast playing Super Mario 64. I would walk over to the stores over and over to play something on that system. It seems to me that someone messed up somewhere. I know that other people have been able to try out other games, but somehow the one store I go into had such a limited ability to market to me. I WANTED to try out things, but my options were limited. I couldn’t even check out some of the networked features that the menu promised would be there if the network was only configured.

In summary:

The Wii: Promising.
Marketing the Wii: Not so much.

Categories
Game Development Geek / Technical General Politics/Government

Games 4 Girls Competition Registration Is Open

In an effort to attract more females to computer science, a male-dominated field, the Games 4 Girls competition asks college women to create computer games designed to be fun for middle or high school age women.

In previous competitions, Game Maker was required, but this year other tools and platforms can be used. The only requirement is that the games can run on Windows XP (the competition is partly sponsored by Microsoft, after all).

This past year’s winners were Cornell University’s Green, Eggs, and Pan, the University of California-Irvine’s Eterative Tale, and North Central College’s DummerUnfall. Honorable mention went to Fluff, created by the team from University of Buffalo.

For this year’s competition, each member of the winning team will be awarded $1,000. Second and third place team members will receive $500. Also, three teams will win $1,000 for their Women’s Club/Organization. If you are a female college student, and you’re interested in the competition, the registration date is December 22nd, 2006. Deadlines and general information about the competition can be found at the Games 4 Girls site.

Categories
Games Geek / Technical

On Games Podcast

Recently I learned about a new podcast: On Games.

While the hosts, Charcoal and Makka, do discuss video games in general, they have an emphasis on indie games. They are definitely opinionated, but I find that it adds to the fun. They love responding to hate mail, for example, although they haven’t received any yet.

As of this writing, they have two shows up, but it appears that we can look forward to weekly updates.