Shuffling values in an array

  • Hi there,

    I got a shuffle-algorithm like this one: https://www.scirra.com/tutorials/599/simple-array-shuffle

    local var: pick=0

    1| Event: System | Repeat Array_Temp.Width times

    2| Action: Array_Temp | Set value at loopindex to loopindex

    1|Event: System | Repeat Array_Music.Width times

    2|Action: System | set pick to floor(random(Array_Temp.Width))

    3|Action: Array_Music | Set value at loopindex to Array_Temp.At(pick)

    4|Action: Delete index pick from X axis

    Problem is, I want to be sure that the values in the array don't match their element-index, which can easily happen with the algorithm above. So i wrote a small addition:

    local var: pick=0

    local var: loopindex_repeat

    1| Event: System | Repeat Array_Temp.Width times

    2| Action: Array_Temp | Set value at loopindex to loopindex

    1|Event: System | Repeat Array_Music.Width times

    2|Action: System | set pick to floor(random(Array_Temp.Width))

    3|Action: System | set loopindex_repeat to loopindex

    4|-- Event: System | While

    System | loopindex_repeat = Array_Temp.At(pick)

    5|-- Action: System | set pick to floor(random(Array_Temp.Width))

    6|Event: 'blank'

    7|Action: Array_Music | Set value at loopindex to Array_Temp.At(pick)

    8|Action: Array_Temp | Delete index pick from X axis

    This seems to do the trick, but:

    1) why do i need to floor the random when setting the variable? doesn't seem to make a difference without it.

    2) possible bug: could this resolve in an infinite loop when the only value in Array_Temp left matches the last index i want to fill in Array_Music (in my case index=7, value=7 since the array-width is 8)?

    3) i declared the variable loopindex_Repeat, cause i'm not sure if the loopindex-expression in the while-loop referes to the while-loop or the repeat-loop.

  • You could do it like this. It first sets the array so the values don't match the indexes. Then it picks a random index and swaps the values if it doesn't make a value match an index.

    repeat array.width times          | array: set at loopindex to (loopindex+1)%array.width
    
    global number index2=0
    global number tmp=0
    
    repeat 10 times                   |
    repeat array.width times          | set index2 to int(random(array.width))
        index2 <> array.at(loopindex) |
        loopindex <> array.at(index2) | set tmp to array.at(index2)
                                      | array: set at index2 to array.at(loopindex)
                                      | array: set at loopindex to tmp[/code:2p48jvha]
  • hi,

    thanks for the reply. could you post a screenshot of the code since i can't clearly see, what event triggers what action.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • +----------------------------------+
    | repeat array.width times         | array: set at loopindex to (loopindex+1)%array.width
    +----------------------------------+
    
    global number index2=0
    global number tmp=0
    
    +----------------------------------+
    | repeat 10 times                  | set index2 to int(random(array.width))
    | repeat array.width times         |
    +----------------------------------+
        +------------------------------+
        |index2 <> array.at(loopindex) | set tmp to array.at(index2)
        |loopindex <> array.at(index2) | array: set at index2 to array.at(loopindex)
        |                              | array: set at loopindex to tmp
        +------------------------------+[/code:1bm3id39]
  • ok. thanks!

    this works like a charm!

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