Fix random range edge conditions and add comments for clarity