0 Favourites

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

  • 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.

  • 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.

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

    Thank you

  • 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.

  • 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

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • 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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
Similar Topics Posts Views Last Post
Unread hot topic
167 14,485
DMT2005's avatar
DMT2005
Unread sticky
1 Favourites
C2 Games made with CocoonIO
136 89,808
minhcuong's avatar
minhcuong
Unread hot topic Locked topic
83 19,081
Kyatric's avatar
Kyatric Construct Team Support