import numpy as np # numpy's random is usualy much faster import random OUT_FOLDER= "./data" LINE_COUNT=500001 CHUNK_SIZE=5000 category_list=list("ABCD") for file_num in range(5): with open(OUT_FOLDER+f"/rand_{file_num}.csv","w"): i=0 while i < LINE_COUNT: i+=min(CHUNK_SIZE, LINE_COUNT-i) numbers=np.random.random_integers(0,4)