Is there a way to directly sample a matrix of random integers that are unique on each row? Doing this for each row apart can be slow.
import random as rd import pandas as pd N = 1000000 # number of rows/number of draws (try N=1000) M = 100000 # range to sample from K = 3 # size of each sample # note: K<=M numbers = pd.DataFrame(columns=['A', 'B', 'C'], index=range(N)) for i in range(N): numbers.iloc[i,:] = rd.sample(range(M),K) # duration in seconds (M=100) # N 1000 10.000 100.000 1.000.000 # method in question 2.2 3.3 13 99 # method by Nin17, 0.0085 0.1 0.57 5.6 # i.e. list comprehension [rd.sample(range(M),K) for _ in range(N)]
Advertisement
Answer
List comprehension is faster if N, M and K are large:
numbers = [rd.sample(range(M), K) for _ in range(N)]