diff --git a/res/res_musiconhold.c b/res/res_musiconhold.c
index 15267d48786b33d50ebeac45b5ce44ea565f1fe1..17ee7f5f1ff0a3b39d437a91ee15a10512275a78 100644
--- a/res/res_musiconhold.c
+++ b/res/res_musiconhold.c
@@ -218,6 +218,8 @@ static int ast_moh_files_next(struct ast_channel *chan)
 			if (ast_test_flag(state->class, MOH_RANDOMIZE))
 				state->pos = ast_random();
 
+			state->pos %= state->class->total_files;
+
 			/* check to see if this file's format can be opened */
 			if (ast_fileexists(state->class->filearray[state->pos], NULL, NULL) != -1)
 				break;