Random value

Discussion and feedback on Construct 2

Post » Tue Sep 06, 2011 10:18 pm

How do I get a random value without decimal point ?
B
21
S
5
G
5
Posts: 163
Reputation: 5,028

Post » Tue Sep 06, 2011 10:24 pm

Try using int(x) where x is the expression you use to get a random value.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,580

Post » Tue Sep 06, 2011 10:27 pm

or round(random(X)) ;)
ImageImageImageImage
B
140
S
58
G
36
Posts: 2,547
Reputation: 31,170

Post » Tue Sep 06, 2011 10:31 pm

Rounding can make the probabilities uneven - round(random(2)) will give you:

0.0 to 0.5: 0
0.5 to 1.5: 1
1.5 to 2.0: 2

1 is twice as likely to come up as 0 or 2.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,580

Post » Tue Sep 06, 2011 10:34 pm

Thank you !
B
21
S
5
G
5
Posts: 163
Reputation: 5,028

Post » Tue Sep 06, 2011 10:34 pm

yes thats true, but isn't random act like that? you can have 1 everytime, or never. You never know, thats pure luck ;P
ImageImageImageImage
B
140
S
58
G
36
Posts: 2,547
Reputation: 31,170

Post » Tue Sep 06, 2011 10:44 pm

If you use random usually you want equal weighting, i.e. each number is as likely as any other to come up. For example:

int(random(2)) will give:
0 (50% of the time)
1 (50% of the time)
Note both have an equal chance of appearing.

round(random(2)) will give:
0 (25% of the time)
1 (50% of the time)
2 (25% of the time)
Not only is the range different, but different numbers have different chances of appearing. In the long run you'll get twice as many 1's as you probably expected. Like I say, usually you want all numbers to appear roughly as many times as each other in the long run, so int() or floor() are probably better here.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,580

Post » Tue Sep 06, 2011 10:49 pm

Can't beat that explanation :D
ImageImageImageImage
B
140
S
58
G
36
Posts: 2,547
Reputation: 31,170

Post » Tue Sep 06, 2011 10:55 pm

I ussually use floor(random).
Maybe has the same effect that int()
B
16
S
3
G
6
Posts: 19
Reputation: 3,204

Post » Wed Sep 07, 2011 12:26 am

It nearly does - floor() returns a float of the number rounded down, and int() returns the number rounded down and converted to an integer. For most uses though they're identical.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,580

Next

Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 12 guests