Shuffling values in an array

Get help using Construct 2

» Sat Jan 23, 2016 3:37 pm

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.
B
3
Posts: 3
Reputation: 181

» Sat Jan 23, 2016 10:48 pm

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.
Code: Select all
`repeat array.width times          | array: set at loopindex to (loopindex+1)%array.widthglobal number index2=0global number tmp=0repeat 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`
B
98
S
36
G
132
Posts: 5,524
Reputation: 84,011

» Mon Jan 25, 2016 1:06 pm

hi,
thanks for the reply. could you post a screenshot of the code since i can't clearly see, what event triggers what action.
B
3
Posts: 3
Reputation: 181

» Mon Jan 25, 2016 5:25 pm

Code: Select all
`+----------------------------------+| repeat array.width times         | array: set at loopindex to (loopindex+1)%array.width+----------------------------------+global number index2=0global 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    +------------------------------+`
B
98
S
36
G
132
Posts: 5,524
Reputation: 84,011

» Tue Jan 26, 2016 11:48 am

ok. thanks!
this works like a charm!
B
3
Posts: 3
Reputation: 181