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:

    3-2-5-4-1

    1-4-2-3-5

    2-5-1-4-3

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

  • 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

    Random(0,5)[/code:rejosrjp]
    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.
    [code:rejosrjp]round(random(0,5))[/code:rejosrjp]
    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:
    [code:rejosrjp]floor(random(0,5))[/code:rejosrjp]
    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.
    [img="https://s27.postimg.org/48owzty9f/random.png"]
    
    Hope that helps.
  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks, Matt and blackhornet.

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