How do I come up with a random sequence of unique numbers?

0 favourites
  • 4 posts
From the Asset Store
Easily generate many levels from a set of pre-built scenes (Construct 3 template)
  • I need to arrange the items in my array in a random sequence. How do I come up with a random sequence?

    Eg: In my game, I will need to arrange 24 items. If only have 5 items to arrange, sample random sequences are:




    I believe that if I can have a way to random list the number 1 to 24 in a non-repeating sequence, I can arrange the items following the sequence. However, someone may have an even simpler suggestion.

    (I'm new and have been looking and trying things out but no luck so far...)

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • To generate random numbers in a variable you can use the Set Variable command and instead of putting a number in use the code "Random(START,END)".

    So presuming you want random numbers between 0 and 5, you would use

    However, this will generate any random float between 0 and 5, so it allows for things like 0.1, 1.5, 2.3, etc.
    to combat this you can add an additional parameter in.
    this will round the float up to a whole number.
    0.4 becomes 1. 
    1.5 becomes 2.
    2.3 becomes 3.
    If you want to round the number down you can use:
    This will floor the number.
    0.4 becomes 0.
    1.5 becomes 1.
    2.3 becomes 2.
    In order to check if there is any repeats, you could store the generated number in a separate variable and then run a check after each number generation to see if it exists, if it does, then re-run the generation code.
    As an example, this is how you would set a text box to a random array value.
    Hope that helps.
  • Thanks, Matt and blackhornet.

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