# Random value

Discussion and feedback on Construct 2

### » Tue Sep 06, 2011 10:18 pm

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

### » 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
414
S
245
G
92
Posts: 25,207
Reputation: 200,355

### » Tue Sep 06, 2011 10:27 pm

or round(random(X)) ;)
B
160
S
69
G
44
Posts: 2,611
Reputation: 36,844

### » 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
414
S
245
G
92
Posts: 25,207
Reputation: 200,355

### » Tue Sep 06, 2011 10:34 pm

Thank you !
B
22
S
5
G
5
Posts: 163
Reputation: 5,058

### » 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
B
160
S
69
G
44
Posts: 2,611
Reputation: 36,844

### » 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
414
S
245
G
92
Posts: 25,207
Reputation: 200,355

### » Tue Sep 06, 2011 10:49 pm

Can't beat that explanation :D
B
160
S
69
G
44
Posts: 2,611
Reputation: 36,844

### » 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

### » 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
414
S
245
G
92
Posts: 25,207
Reputation: 200,355

Next