Generating unique random value in Numpy

Viewed 3007

I tried to generate large numbers of unique random values using np.random.randint but it returned few duplicates values.

I have following dataset

District    Prefix  Quota       
A           98426   783
A           98427   223
A           98446   127
A           98626   51
B           98049   167
B           98079   153
B           98140   120
B           98159   139
B           98169   182
B           98249   86
B           98426   588
B           98446   96
C           98049   104
C           98060   68
C           98149   65
C           98150   68
C           98159   86
C           98160   80
C           98169   113

Code to reproduce:

import pandas as pd
df = pd.DataFrame([
    ['A', 98426, 783],
    ['A', 98427, 223],
    ['A', 98446, 127],
    ['A', 98626, 51],
    ['B', 98049, 167],
    ['B', 98079, 153],
    ['B', 98140, 120],
    ['B', 98159, 139],
    ['B', 98169, 182],
    ['B', 98249, 86],
    ['B', 98426, 588],
    ['B', 98446, 96],
    ['C', 98049, 104],
    ['C', 98060, 68],
    ['C', 98149, 65],
    ['C', 98150, 68],
    ['C', 98159, 86],
    ['C', 98160, 80],
    ['C', 98169, 113]
], 
    columns=['District', 'Prefix', 'Quota'])

I had to create a unique random number and add it to the prefix. the number of random values is given in Quota

np.random.seed(1)  # for repeatability 
random=df.Prefix.repeat(df.Quota)*100000 + np.random.randint(0, 99999, df.Quota.sum())

I thought np.random.randint gave unique numbers but while generating around 18000 numbers, it gave around 200 duplicate number. eventually I tried random.sample and problem was fixed. but I want to generate unique numbers using np.random.randit because I can change seed in np.random.seed(n) and can create another set of unique numbers different from first set by changing seed.

1 Answers

You won't be able directly with np.random.randint, since it doesn't offer the possibility to randomly sample without replacement. But np.random.choice does. And by specifying a random seed, you can reproduce the generated sequence, which will consist on a random, uniformly sampled distribution array within the range range(99999):

np.random.seed(2)  
numbers = np.random.choice(range(99999), size=df.Quota.sum(), replace=False)
random = df.Prefix.repeat(df.Quota)*100000 + numbers

len(random)==random.nunique()
# True
Related