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

Discussion and feedback on Construct 2

Post » Tue Jul 18, 2017 4:24 am

Hi!

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

Image

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?
B
5
S
1
G
1
Posts: 5
Reputation: 835

Post » Tue Jul 18, 2017 5:29 am

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.
ImageImageImage
B
71
S
22
G
248
Posts: 3,760
Reputation: 138,071

Post » Tue Jul 18, 2017 6:37 am

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.
If your vision so exceeds your ability, then look to something closer.
Moderator
B
136
S
31
G
86
Posts: 5,489
Reputation: 59,764

Post » Tue Jul 18, 2017 8:59 pm

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

the '3 + 1' makes it readable.
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Thu Jul 20, 2017 11:08 am

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!
B
5
S
1
G
1
Posts: 5
Reputation: 835

Post » Thu Jul 20, 2017 4:26 pm

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.
Image ImageImage
B
7
S
2
G
1
Posts: 93
Reputation: 1,243


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 7 guests