Let’s Look At: Random Numbers

This is something a little different from my usual examples.  I’m going to take you through Construct 2’s random number expressions, how they work and how we can manipulate them to our advantage when creating a game.

Click below to read the whole post.

Firstly lets look at C2’s random number expressions, then later, we’ll look at how we can manipulate them for our needs.

Random(X) – This is the most basic random expression in C2.  To use this we simply replace the “X” in the brackets with a number.  C2 will then generate a random number between 0 & “X” but not including “X”.

So what does that mean?

Well let’s replace “X” with 10.  Our expression is now random(10).  C2 will now generate a random number between 0 & 10 but not including 10.  Another thing to note is that the number generated will be a float or in simpler terms a decimal number.

Here are 10 random numbers that I generated in C2 using random(10).

6.8455614476, 0.2660379512, 1.8084836775, 6.2771765306, 3.0607472965, 8.2386168512, 3.4278223943, 7.2873926791, 1.6512241215, 3.5610843915

Random(a,b) –  This expression now lets us generate a random number between a & b but not including b.

Again let’s look in a little more detail.  Lets replace a with 10 and b with 20.  Our expression is now random(10,20).  C2 will now generate a number between 10 & 20 but not including 20.  Again the number generated will be a float.

Here are 10 random numbers that I generated in C2 using random(10,20).

13.1834654487, 12.682121573, 19.7562856413, 15.1425380167, 11.9350582873, 19.0866457392, 10.1437296323, 16.2661679252, 19.0550344018, 14.185810599

Before looking at the third way of generating random numbers let’s take a look at how we can manipulate the numbers generated using random(X) and random(a,b).

It’s not very often that a project will require a randomly generated number with 10 decimal places, usually what we’d want was whole numbers (integers).  C2 has three expressions that easily allow us to convert a float into an integer, it’s just a matter of picking the correct one for our needs.

Floor() – Will round down a float to the nearest integer.  For example floor(5.6) would round down to 5.

Round() – Will round up or down to the nearest integer.  For example round(5.6) would round up to 6, while round(5.3) would round down to 5.

Ceil() – Will round up to the nearest integer.  For example ceil(5.2) would round up to 6.

Let’s use one of our new expressions and add it to random(10) to create some more random numbers .  Here are 10 numbers generated using ceil(random(10)).

6, 7, 2, 2, 10, 3, 8, 6, 5, 6

Excellent!  Gone are our 10 digit decimal floats and we end up with some nice integers.

But this has given us a problem!

Remember from earlier we learned that random(10) generates a number from 0 & 10 but NOT including 10.  If you look at the list of numbers you’ll see that 10 is included in the list.

Why is this?

It’s simply down to our rounding method.  Random(10) still generates a number between 0 & 10 not including 10 but we are rounding that number up.  So if random(10) generates 9.0094645182 and we use ceil() to round up.  Our interger will become 10.

Just keep in mind when using floor(), round() and ceil() how they’ll effect the numbers generated for your project.

Now let’s say that you want your generated number to be to 2 decimal places.  C2 doesn’t have any built in expressions to do this, but using a tiny bit of Math you can convert your 10 decimal place float to a 2 decimal place float.  You do that by using the following round(yourvalue*100)/100 and simply replace yourvalue with the number you wish to covert.

Here are 10 randomly generated numbers using round((random(10))*100)/100.

0.48, 3.09, 2.82, 2.79, 9.49, 6.62, 8.36, 7.99, 3.16, 1.07

You can do this for any number of decimal places by using this formula as the number of 0’s represents the number of decimals.  For example round((random(10))*10000)/10000 will round the decimals to 4 decimal places.

2.9026, 8.9499, 9.3635, 9.9643, 7.3747, 8.3462, 0.3388, 2.2167, 4.8356, 9.897

Now lets look at one of my favourite expressions in C2.  The choose() expression and how we can use it for random numbers.

Choose(a,b,c) –   C2 will pick a value at random from a comma separated list.  There can be infinite values as long as there are 2.

Again let’s look at this in more detail.  First let’s build a valid choose() expression.  Choose(0,1,2,3,4,5,6,7,8,9,10) now C2 will choose a value at random from our list.

Here I’ve used Choose(0,1,2,3,4,5,6,7,8,9,10) to generate 10 numbers.

8, 9, 6, 9, 6, 5, 3, 6, 10, 5

It’s important to note that unlike random() the choose() expression also works with strings, for example Choose(“Paul”,”John”,”Bob”) C2 would randomly choose Paul, John or Bob.

Let’s look how we can make choose() more useful and more powerful for a game.  Using the choose() expression we can generate a weighted random number.

Imagine we are creating a basic RPG.  We have a monster that when killed could drop 5 different normal items and 1 rare item.  We could achieve this easily by using the choose() expression.


As you can see the chances of choosing 0, 1, 2 , 3 or 4 are the same while 5 or our rare item has a much smaller chance of being randomly selected.

Here are 100 randomly generated numbers using our expression choose(0,0,0,0,0,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5).

0, 1, 2, 4, 1, 1, 3, 3, 4, 3, 2, 3, 3, 2, 1, 3, 0, 1, 3, 4, 3, 4, 4, 1, 3, 2, 1, 3, 1, 0, 4, 2, 1, 2, 2, 3, 3, 5, 3, 1, 2, 4, 0, 3, 1, 3, 2, 2, 0, 3, 5, 4, 1, 0, 4, 1, 3, 0, 4, 4, 3, 0, 0, 0, 4, 3, 0, 2, 4, 5, 2, 2, 0, 1, 4, 1, 3, 3, 4, 3, 1, 2, 1, 0, 4, 1, 4, 1, 0, 0, 4, 4, 3, 1, 2, 4, 3, 2, 3, 0

As you can see from the numbers above if we killed 100 monsters we would only get 3 rare item drops.

And that’s the basics of random numbers.  I hope you find it useful.  If you do please leave a rating for the post and I’ll create more of these types of posts.


3 thoughts on “Let’s Look At: Random Numbers”

  1. That was an awesome tutorial I hope you do more in the future, two years using C2 and I didn’t know all these things, Thanks a lot man

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s