Fixed a hang bug

With the grid system, sometimes the random placement would set the
element outside of the array. It should work fine now.