Get a random number

This forum is currently in read-only mode.
From the Asset Store
Easily generate many levels from a set of pre-built scenes (Construct 3 template)
  • First of all I want to congratulate the developers of construction and this beautiful community!

    I have a small problem that bothers me really, I'd get a random number between -5 and 5, is this possible? The "get a random number" fonction does apperence as to draw a between 0 and XXX.

    Thank you in advance for your help! (and sorry for my english.)

  • How about: random(10) - 5. Would that work?

  • -5+random(10) is what I use, it's the same as shadowlord posted, just reads better for me.

  • Thank you very much, it's perfect !

  • clamp(normalrandom(0,5),-5,5)

  • I use 5-Random(11) for cases like this.

  • I use 5-Random(11) for cases like this.

    And it is good that you do so, because random(integer) returns floor(random), or in other words:

    random(10) produces a number greater than 0 and less than 10 (e.g. 0.024 or 9.8453) and returns it rounded down to the nearest integer (0.024 will be returned as 0, 9.8453 as 9)

    As a result, random(10) will return one of 10 numbers ranging from 0 to 9.

    -5 + random(10) would be -5 + [0, 9] => [-5, 4]

    random(10) - 5 would be [0, 9] - 5 => [-5, 4]

    to reach the full range of [-5, 5] (which is 11 numbers) you need random(11) (which returns in a range of 11 numbers from 0 to 10):

    5 - random(11) would be 5 - [0, 10] => [5, -5]

    random(11) - 5 would be [0, 10] - 5 => [-5, 5]

    -5 + random(11) would be -5 + [0, 10] => [-5, 5]

    The order doesn't make a difference, so use whatever looks good to you, just make sure the full range is covered <img src="smileys/smiley1.gif" border="0" align="middle" />

  • Thank you for this clarification!

  • How to randomly generate a whole number?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Hi matcon, here's for a random integer (whole number): int(random(10))

  • Hi matcon, here's for a random integer (whole number): int(random(10))fqm@Jayjay

    It doesn't hurt to do it that way, but it isn't needed. CC automatically returns a number based on the input. When using an integer the return value will be an integer also, when using a float the return value will be a float.

    random(10) will return an integer in the range [0, 9] -> 0, 1, 2, ..., 8, 9

    random(10.0) will return a float -> 0.015, 3.4127843, 7.1095284, etc.

    <img src="smileys/smiley2.gif" border="0" align="middle" />

  • tulamide, ah cool, thanks for sharing that <img src="smileys/smiley4.gif" border="0" align="middle" />

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