getting only full numbers with random function

0 favourites
  • 7 posts
From the Asset Store
Easily generate many levels from a set of pre-built scenes (Construct 3 template)
  • hello im trying to get a random age to generate between 1 and 100 but it generates numbers line 2.74637 and 1.93382927 how do i get full numbers?

    Tagged:

  • int(random(1,100))

  • thanks

  • (int(random(1,101)) if you want to include 100.

  • I would do round(random(1,100))

    that way any number 99.5 and above will be set to 100

    technically 1 and 100 have a less chance of coming up if you want every number to have an equal chance maybe:

    round(random(0.5,100.49))

    I think int just strips away the decimals.

  • Int does removes the decimals. It effectively rounds towards 0.

    Floor rounds towards negative infinity.

    Ceil rounds towards positive infinity.

    Random gives you any number from the lower bound up to but not including the upper bound.

    Round(random(0.5,100.5)

    Floor(random(1,101)

    Int(random(1,101)

    Floor(random(100))+1

    Int(random (100))+1

    Are all equivalent, use whatever whatever you like.

    Ceil(random(100)

    Is also basically the same thing, except you have a negligible chance of actually getting 0. And since I'm being needlessly pedantic now, I believe JavaScript uses a 53 bit float, so the actual chance world be around 1 in 90 quadrillion or so.

    int(random(1,100)) can never result in a 100 though, while round(random(0.5,100.49)) has a 1% less chance to get 100 compared to 1-99, and round(random(1,100)) would have half the chance of getting 1 or 100 compared to the other numbers.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • oosyrag wow that is thorough! :)

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)