How to use random() efficiently

0 favourites
  • 8 posts
From the Asset Store
Easily generate many levels from a set of pre-built scenes (Construct 3 template)
  • Normally I use "choose()" but now I've to use "random()" because it will attend me in several layouts (eg: int(random(number, var))). The problem with random is the constant repetition of numbers. When I use "choose()", I usually include copies of the numbers or strings (eg: 1,2,3,4,5,5,4,3,2,1,1,3,5,2,4) and the possibility of repeating numbers is not very common. But now I need to use "random()" and would like to know if anyone have a simple solution to improve it.

    Thanks in advance

  • I'm not sure what you mean..

    int(random(number, var) just chooses a random number between your number and your var not including var

    random(a, b)

    Generate a random float between a and b, including a but not including b.

  • Is the same thing that you wrote with the difference that I use a variable to define the random range. The problem of the "random()" is the repetition of the same number more frequently than "choose()", at least was that I've observed (eg number = random(0,5) > number = 5, number =5, number =3, number =5, ...). So, to prevent repetition when I use "choose()" I usually include several copies of these numbers in different positions like this: chosse(1,2,3,2,3,1,3,1,2). This works for me.

  • It shouldn't make any difference..


    is the same as


    is the same as


    Random(0,5) will give any number between 0 and 4.999999

    if you are going to round it,

    0 will only be if the number is between 0 and 0.499999),

    but 1 will be between (0.5 and 1.49999)

    So yes there is twice as much chance of 1 than there is of 0

    I would use floor(random(0,variable+1) that way all numbers from 0 to variable have the same chance of being selected..

    0 (from 0 to 0.99999)

    variable(from variable to variable+0.99999)

  • Thanks LittleStain

    I'll give it a try.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Example

  • I use the following:

    "floor(random(x+1))" if you need a range between 0 and X.


    "floor(random(b-a+1))+a" if you need a range between A and B.

  • LittleStain - Thanks for the example.

    - Nice solutions. I'll try it.

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