# Random number int(random(0, 3) doesn't reach 3

0 favourites
• 6 posts
From the Asset Store
Easily generate many levels from a set of pre-built scenes (Construct 3 template)
• Hi!

Just a short question about random number generation. First, please take a look at this line:

Every 0.1 seconds, this line converts a randomly generated number, between 0 and 3, from float to int... or does it not?

My question was, why it nearly never will reach number 3, while you observe it.

Am I right about this? -The number will nearly never be three because the computer is generating floats that will be rounded down to a whole number. Which means, the computer cuts down everything behind the period (for example: 2.3764763547 = 2 or 0.4657623846 = 0). This means, that I need to set the variable to int(random(0, 4)) to get a 3 by times.

Am I right?

• Correct. The documentation clearly states how random() works:

random(a, b)

Generate a random float between a and b, including a but not including b.

• As blackhornet has said, this is covered in the Manual.

Expressions in the Manual are very important - it is easy to just skip over when reading the Manual.

When in doubt, try re-reading this section. Very useful and powerful commands.

• Use floor(random(0, 3 + 1))

the '3 + 1' makes it readable.

• Thank you very much for your replies,

I really was reading the manual, but as zenox98 said, I've just skipped it, while reading it. Sorry for that, but thank you for your hints!

• ## Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• https://c2tutorials.wordpress.com/2014/10/26/lets-look-at-random-numbers/

You should check out that webpage, it tells you basically all you need to know about random numbers in detail.

I've found it very helpful. I'm sure others will too.

• 6 posts