As I've mentioned in my post about Browser Game Basics, the main difference between a conventional web application and a browser game is its non-deterministic (or random) behaviour.

For example, if you are writing a ToDo-app, your users don't want any funky behaviour. They write a todo item, they save it, and they expect to find it again in the same form the next day. This is deterministic behaviour. You can already determine what the outcome of an action is.

Games Are Different

This is not the case for most modern games, as they employ random number generators (RNG) to determine the outcome of something. Want to find how much damage you deal to a monster? Pick a number between your minimum damage and maximum damage. Did the monster dodge your attack? Let the RNG decide.

Don't be fooled. Random number generators are - in my opinion - the single most important underlying technology for an engaging game. No player wants to do the same thing over and over again. Throw in a rare reward every now and then. A large amount of gold, a cool sword, a temporary powerup. This will keep players interested ("I wonder which item I'll get next.") With random behaviour, you can do that. Make random numbers your friend! They are plain awesome.

What Can We Randomize?

It may be a bit more involved to understand how an RNG works, but it's fairly simple to use one. Most modern languages offer functions to draw random numbers.

  • PHP offers the mt_rand() function
  • Python has random.random()
  • In Ruby it's calledRandom.rand()

In terms of what you can randomize, your imagination is your limit. Just make sure it makes sense for the game.

Below, I'll explain some of the most common use cases for random numbers. For the examples, I won't be using a specific programming language or syntax, but rather try to describe it understandably.

Equal Probability

This is the most common case and is often used when determining (raw) damage. Let's say, your character's weapon does 5 to 10 damage. To determine the damage, you simply pick a number between the lower and the upper bound.

damage = random_integer(min_damage, max_damage)

Chance for an Event to Happen

Use this when an event has a certain (percentage) chance to occur, for example a critical hit or dodging an attack. Let's say you have a 5.25% chance to dodge a monster's attack. You can randomise a float between 0.0 and 1.0, multiply it by 100 to get a percentage, then check if the chance to dodge is greater or equal to your float. This means that 5.25% of all drawn floats will be lower than your chance. These are the cases where you have successfully dodged the attack.

chance_to_dodge = 5.25 // in percent
 if (chance_to_dodge >= rand_float()*100.0) {
   dodged = true
}

If you want multiple events that can happen simultaneously (dodge + counter-attack), just make sure you get a new random float for each event. For example:

chance_to_dodge = 5.25 // in percent
 chance_to_counter = 12.0 // in percent, only after you dodged

 if (chance_to_dodge >= rand_float()*100.0) {
   dodged = true
    if (chance_to_counter >= rand_float()*100.0) {
       counter_attacked = true
   }
}

Notice the two separate calls to rand_float().  The probability to dodge is still 5.25%, but if you dodge, you have a 12% chance to counter-attack. Since these events are depending on each other, we need to multiply their chances to get the total chance of dodging+countering: (5.25%*12.0%) = 0.63%

Watch out for these "dependent" probabilities.

Picking from Multiple Choices

In some cases, you want to pick one of multiple events which can happen, maybe even with different probabilities. This can be useful, if your player opens a chest (or a Gift Box in Directive Alpha) and you want to determine what they find inside. Finding money in a chest is common, but the shimmering armor is quite rare.

One way to achieve this, is to assign each event a weight. This weight represents the probability of an event happening relative to the other events that can happen. The higher it is, the more likely it is.

Let's say you have three events, that bestow money on the player:

  • Normal amount of money, weight 750
  • Large amount of money, weight 200
  • Huge amount of money, weight 50

If you sum up these weights (which can be arbitrary), you get a total weight of 1000. Then generate a random integer between 1 and 1000 to determine which amount of money the player gets.

  • If the number is between 1 and 750, they get a normal amount,
  • If the number is between 751 and 750+200=950, they get the large amount,
  • If the number is between 951 and 1000, they get the huge amount.

To determine what the probability is of a specific event occuring is to simply divide its weight by the total weight. So for the large amount, this would be 200/1000*100% = 20%.

Note: For a more detailled explanation, see my article about How Gift Boxes Work in Directive Alpha.

If you want several events which can happen with equal probability, don't use this method and set equal weights for all of them. Use the "Equal Probability" method above.

Conclusion

There are a plethora of ways to randomize aspects of a game. Use it to your advantage. If used correctly, randomization can create many engaging situations for your players.

Side note: I occasionally use random events to hide easter eggs in my games, which can only be discovered with some luck and by playing for a while. An example for such easter eggs are the named celestial bodies which you can find in Directive Alpha with your radar.

I'm making the space browser game Directive Alpha. If you haven't started playing yet, go to www.directivealpha.com and sign up now!