Weighted choice in Construct 3

Most user of Construct 3 are familiar with the choose system expression. It’s a simple way to add a bit of randomness by having the computer pick between a set of values (that is, parameters). Here is how the manual describes the expression:

choose(a, b [, c…])Choose one of the given parameters at random. E.g. choose(1, 3, 9, 20) randomly picks one of the four numbers and returns that. This also works with strings, e.g. choose("Hello", "Goodbye") returns either Hello or Goodbye. Any number of parameters can be used as long as there are at least two.

System expressions – Construct 3 Documentation

But what the manual doesn’t say, and many users may not realize, is that the parameters in the expression do not have to be unique. In other words, you can skew the random choice by putting in more of some values than others. For example, if you want an event to cause a small amount of damage to the player, or no damage at all in most cases, you could write the expression this way: choose(0,0,0,0,1,1,2,3). There would be a 50% chance of zero damage, a 25% chance of one point of damage, and a 12.5% chance each of two or three points of damage. Here’s how this would look in a Construct 3 event sheet:

Another place to use weighted choice would be in spawning events. A single spawner object could create various types of units, friendly or enemy, depending on the result of a weighted choice expression. So, for example, here is a timer event that uses weighted choice:

In this example, the spawner object has an instance variable named “whatToSpawn.” The event will produce rocks more often than coins, but coins more often than fireballs. Note there is also a small chance of nothing at all being produced if the result of the choose expression is zero.

Finally, here’s a link to a small Construct 3 program that demonstrates weighted choice in action: ChooseDemo (anderfamgames.com)

This is the complete event sheet for the demo: