Random(x,x), how to make it choose integers over doubles

Get help using Construct 2

Post » Wed Nov 16, 2016 8:23 am

Hey! So basically, I am making a game where a selection at the beginning of the game is required. I initially wanted to make it so I could use the random(parameters) function would work; however the issue is I the random function can return a result of a double (2.5 for example), when I'm really requiring it to return an integer only. Is there any way possible or do I have to go through the tedious practice of putting in conditions of > than and < than?

Thank you.
B
4
Posts: 22
Reputation: 244

Post » Wed Nov 16, 2016 9:17 am

I'm not really sure i understood, but you could try something like this.

if you only want integers returned you can use round, floor, or ceil.

round(random(1,10)) will give you integers between 1 and 10
floor(random(1,10)) will give you integers but rounded down. 2.5 will be 2
ceil(random(1,10)) will give you integers but rounded up 2.5 will be 3

Was that what you were looking for?

Otherwize you can use the choose instead.
choose(1,2,3,4,5,6,7,8,9) or how many parameters you want. it will pick a random one of those.
Last edited by tunepunk on Wed Nov 16, 2016 9:24 am, edited 1 time in total.
Follow my progress on Twitter
or in this thread Archer Devlog
B
40
S
17
G
17
Posts: 991
Reputation: 12,654

Post » Wed Nov 16, 2016 9:21 am

round(random(1,10)) looks right, will post back when I implement it!

Thank you
B
4
Posts: 22
Reputation: 244

Post » Wed Nov 16, 2016 9:25 am

IN case you missed the edit:

Otherwize you can use the choose instead.
choose(1,2,3,4,5,6,7,8,9) or how many parameters you want. it will pick a random one of those.
Follow my progress on Twitter
or in this thread Archer Devlog
B
40
S
17
G
17
Posts: 991
Reputation: 12,654

Post » Wed Nov 16, 2016 9:39 am

I am using choose as of now, however the numbers will go up to at least 30-50 soon, so random(1,50) may be more practical
B
4
Posts: 22
Reputation: 244

Post » Wed Nov 16, 2016 10:11 am

tunepunk wrote:IN case you missed the edit:

Otherwize you can use the choose instead.
choose(1,2,3,4,5,6,7,8,9) or how many parameters you want. it will pick a random one of those.



You forgot int() ;)

I would choose random() over choose() for returning a value from a large range. Easier on the eye, easier to work with and probably better performance.
Image
B
30
S
9
G
3
Posts: 171
Reputation: 3,329


Return to How do I....?

Who is online

Users browsing this forum: Phacanu, Yahoo [Bot] and 31 guests