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.

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).

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.

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.