How to shuffle target point locations?

I am trying to shuffle five target points and then assign them as actor locations for five teleporter entrances so that each time I start a game, the one of five teleporter entrances sends you to one of five exits and that exit persists for that teleporter until a new game is started. What I don’t want is to have to network all 120 possible combinations of five teleporter exits.

Is there a way to take 5 variables (in this case, my Target Point locations), scramble/shuffle them, and then output them to new variables??

To put it visually, I am trying to achieve something that takes these 5 variables and outputs them in a random order so that I can network them into my SetActorLocations:

4457-shuffle_target_points.jpg

It can be done, but shuffling can be a bit tricky. The way I usually do it is to splice my numbers into random positions within a new array. After you have your shuffeld array you can use it to put your teleporters wherever you want.

Hey Camoo, how would I randomly splice them into the array without overwriting any of the indices? ie, if I assign each target point to a random index, it would occasionally assign two or more target points to the same indices. How do I set up an algorithm to avoid this while still randomly splicing them to each of five indices?

I am trying to wrap my head around setting up a Fisher-Yates shuffle (Durstenfeld modern version) using blueprints but I am having a hard time. Does someone have an example of this array shuffle set up in blueprint form?

Does Insert overwrite the array element?

If it does, it seems like you would have to retrieve all array information and then refile them back into the array after you make your addition and randomize the order.

The ForLoop shown in the article looks like a ReverseForLoop that I saw someone make a plugin for somewhere on the forums.

I haven’t seen a node that could do “exchange a[j] a[i]” either, so that may be something else requiring a plugin, or some convoluted method to shuffle and randomize an array.

Can you post an image of a blueprint set up that would randomly insert array elelments?

I should note that if you want to drop the “Random Array” back into the “Array”, you could just put a sequence in front of the for loop and have the second output from sequence lead to another ForEachLoop that would transfer the Random Array back to the Array.

Does this really randomize the output? Seems it just stacks items in the same order but using different Add nodes?

The best way to do this depends on the size of your array and how often you need to shuffle it, but if it’s not very often and it’s small a good way to shuffle it is to get two random indices within the arrays length and switch these two around. Do this a couple of times, there’s no need to be precise so just wing it. You don’t need to be looking up algorithms unless it’s a high performance case.

Hopefully you didn’t waste much time with my last picture because it wasn’t quite right. :stuck_out_tongue:

This is a little more complicated than I first envision, so using Diesel’s method may be more efficient.

Hey Zeus, do you mind explaining what I’m looking at here? If I’m not mistaken, you’re taking the elements of Array, inserting them randomly into either RandomArray1 or RandomArray2, then taking RandomArray1’s elements and inserting them into the Array, then taking RandomArray2’s elements and inserting them into the Array?

It seems that this would give a low chance of, say, my 5th element of the original Array showing up as the first index of the output Array since there are 4 other elements with a chance to be inserted into the Array before it?

Can you share an example blueprint for just swapping two random array indices? My array will never be larger than 8 and will only be shuffled once on game start so switching indices maybe five times sounds like a decent amount of randomization. I’ll investigate myself later tonight as well. Thank you for the suggestion.

Yeah essentially it takes your array, randomly puts everything into one of 2 random arrays, and then stacks those arrays on top of each other, back into your original array.

So if you have 10 items, Index 1 could be #1 if it goes into RandomArray1, or it could be #6 if it goes into RandomArray2.

The more times you perform this operation, the more random it will be. If you are worried that your first Index will stay relatively close to the front for a long time, you could add more RandomArrays for better shuffling.

You can put another loop in front of the sequence and have it run X times, if you feel you have enough RandomArrays.

So I’ve hooked this up and it is working wonderfully (note to anyone hooking this up, Min Max on Random Int should be 0,1). I added a final pin to the sequence that printed each arrays element in sequence and it was definitely randomizing the array elements! Success! Thanks Zeus

I also just realized my task may be much more daunting than I had originally perceived. Boo!

When I randomize the teleporter exits, it randomizes them wonderfully in one direction. How can I tie the teleporters traveling back the other way to then connect to the correct origins? All I can think of is needing to map out every possible path using a series of true false branches that check where the random exit came from to decide where to send you back that just makes my head spin. Any simpler ideas?