'n' as a random number and 'n' as an index value are the same variable? That doesn't make sense, or would seem to be a source of unpredictable behavior.

The array RandomValue[] should begin with...