Remove index not working correctly

I have a vector array with 100 unique vectors. I created a loop that runs 100 times. each loop should spawn an actor at a random vector in the array, then remove that vector item from the array so that it cant spawn an actor at that vector again. I run the function and some of the actors are overlapping. Why?. I ran tests to to ensure that at the end of the while loop the array is empty and it is. I also ran tests to ensure that each vector item is unique. So how does it spawn some overlapping actors?

1 Like

What’s here?

image

As always with Randoms:

Evaluation happens twice due to the 2 execution wires. You use one random for placement, but remove another, different random from the array. The solution is to store the index in a variable and use the same index for accessing and removal:


Another solution is to shuffle the array:

No need to remove anything (at least in this very scenario), the elements are randomised inside the array, just get the next one.

3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.