# [go to layout] causing application to close

### » Sat Jun 08, 2013 12:34 am

@Tulamide, ah I found random doesn't spit out nice integers for me like I try to force it into, although the clamp wouldn't solve that either.

And surprised to see random(3) is between 0 and 2, thanks for sharing

"Construct 4 lets YOU make advanced games! (maybe)" Construct Classic - Examples Kit
B
88
S
29
G
13
Posts: 2,098
Reputation: 15,171

### » Sat Jun 08, 2013 9:17 am

[QUOTE=Jayjay] @Tulamide, ah I found random doesn't spit out nice integers for me like I try to force it into, although the clamp wouldn't solve that either.[/QUOTE] @Jayjay: I see. Maybe this could help.
Internally a random function calculates a float that is always greater than 0 (zero) and lesser than the number you hand over. If CC detects a number without fraction part, it returns the calculated float without fraction part, too. That's why the range is [0, i-1]
Throughout CC there's the problem of slight inconsistencies with very small or very high fractional numbers. That's due to the single precision floats CC is based on and can't be avoided. For example, you might experience some number like 1.234e-9 (=0.000000001234) where you expected a 0 (zero).
If you want to have more freedom over the numbers generated by random(), you could use floats instead of integers and do the conversion yourself.

floor(random(3.0)) => integer in the range [0, 2]
round(random(3.0)) => integer in the range [0, 3]
ceil(random(3.0)) => integer in the range [1, 3]

pseudo code
on function "myrandom"
set return value to round(random(param(2) - param(1)) + param(1)

set text to function.myrandom(3, 8) => integer in the range [3, 8]
set text to function.myrandom(2.5, 4.75) => float with integer steps in the range [2.5, 4.5] (2.5, 3.5, 4.5)

Hope it helps :)
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

### » Sat Jun 08, 2013 6:46 pm

@Tulamide Awesome, thanks for explaining that for me
"Construct 4 lets YOU make advanced games! (maybe)" Construct Classic - Examples Kit
B
88
S
29
G
13
Posts: 2,098
Reputation: 15,171

Previous