How do I randomise array elements?

0 favourites
  • 9 posts
From the Asset Store
Advanced inventory mechanics for your RPG game (Array-based). Take Items, split them, pick up them, read the description
  • Hi. I'll try to keep this short but sweet. Variable names and such given here are not the same as what I'm using, and all code-like writing I've written is pretty much only pseudo, in the interest of simplicity.

    I have a string, called 'myWord', which could be a word of any kind. In this example, we'll call it "JUMBLED".

    I also have an array, called 'myArray', which takes each character from 'myWord' and places them in order.

    So, in theory, it looks something like myArray = [J,U,M,B,L,E,D]

    What I desperately need to achieve, is for the contents of myArray to be randomised, (something like myArray = [U,M,E,D,L,J,B]) instead of in the correct order, as it is now.

    The problem is, despite all my research I haven't been able to find any particularly useful solution that doesn't cause a headache.

    Therefore, does anybody know of a simple means to randomise the contents of an array, either when inputting the string value to the array, or after it has already been created?

    Many thanks!


  • This might help:

  • the solution is quite simple:

    1.create an additional row on myArray.

    2.on the second row place your letters

    3.on the first row place random numbers

    4. myArray > sortX new order of letters from 2nd row

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks for your replies, guys.

    MaorKeshet, a problem I perceived with your solution was the same problem I was having with randomly moving letters based on index position: As far as I'm aware it isn't possible to randomly generate numbers up to whatever limit, without randomly picking the same number more than once. So, randomly generating numbers in a list between 0-5 for instance, rather than generating 1,5,2,4,3,0, is far more likely to always produce something like 4,4,2,1,0,0, which isn't particularly helpful. Sure I could still order that as 0,0,1,2,4,4, but ideally in my scenario (particularly as other uses for random generation have come up), I need them to be unique numbers.

    The solution I've gone with in the end is to have three Arrays:

    ArrayLetters [J, U, M, B, L, E, D]

    ArrayTemporary [J, U, M, B, L, E, D]

    ArrayRandom [0, 0, 0, 0, 0, 0]

    • then, with a loop, randomly moving a letter from ArrayTemporary to ArrayRandom, and then deleting said letter from the index of Array Temporary, so that I have something like...

    ArrayLetters [J, U, M, B, L, E, D]

    ArrayTemporary [ ]

    ArrayRandom [B, L, M, J, U, D, E]

    It's by no means ideal, though it works, but I'm not certain it would work with multiple rows in the same array, given that the loop is based on the width of ArrayTemporary, which changes over the course of the loop.

    Basically, as mentioned in the beginning of this post, if I knew of a way to randomly generate numbers without repeating those that have already been generated, it would make the process a hell of a lot easier.

    Many thanks for your suggestions.

  • One method I saw here and used was to have 2 local variables, and use them as temporary storage of values as you loop through the array's cells. For each array element: set variable A to current cell value, then set variable B to a random number (0,arraywidth). Then set current element to value at index of variable B, and set the value at index of variable B to variable A.

    It may sound a little complicated at first, but it's really quite simple.

  • Tobye, I'm yet to try your solution but I'll let you know as soon as I have whether it works or I have trouble.

    Yann, your file says it was created in Construct v164.2 'or later', but the newest version I'm able to find at all is v163. Weird!

  • He is using the beta release

    You can find it here

  • thanks Yann your example helped me.

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