Josh
Josh

Reputation: 99

generate matrix of random integers unique per row in python

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)] 

Upvotes: 0

Views: 511

Answers (2)

Nin17
Nin17

Reputation: 3502

List comprehension is faster if N, M and K are large:

numbers = [rd.sample(range(M), K) for _ in range(N)]

Upvotes: 0

Amir Shamsi
Amir Shamsi

Reputation: 361

one of the ways I know is that you can do it using numpy.

numpy has a function called reshape() its job is to shape the list you want to any shape like matrix.

try this code:

import random as rd, numpy as np
matrix = np.array(rd.sample(range(M), K*N)).reshape(N, K)

in here N is number of rows and the K is number of columns.

Upvotes: 1

Related Questions