Rename R_MallocMedia to R_AllocMeida, same for S_. Also use time(NULL) for media seed rather than Random(), which could in theory produce duplicates.