randomindex refactored