I am using QGIS 2.18.11 and need to select 250 points from the existing points in a map. I know that I can randomly select points through Vector> Research Tools> Random Selection but I don't know how to make it "reproducible" (replicable). I don't know much about Python.

  • 3
    For making reproducible a random selection you need to use in a python script, for instance, 'random.seed' method from numpy (please, see my answer). – xunilk Aug 19 '17 at 4:37

For making reproducible a random selection you need to use in a PyQGIS script, for instance, 'random.seed' method from numpy. Assuming that you have 10.000 points in your layer, next code select same 250 points each time is running (with seed equal 0).

import numpy as np


random_numbers = []
n = 0

while (n < 250):

    number = np.random.randint(0,10000)

    if number not in random_numbers:

    n = len(random_numbers)

print random_numbers

registry = QgsMapLayerRegistry.instance()
Points = registry.mapLayersByName('Points')
feats = [ feat for feat in Points[0].getFeatures() ]
epsg = Points[0].crs().postgisSrid()

selected_features = []

for number in random_numbers:

uri = "Point?crs=epsg:" + str(epsg) + "&field=id:integer""&index=yes"

mem_layer = QgsVectorLayer(uri,

prov = mem_layer.dataProvider()

for i, feat in enumerate(selected_features):



I tried it out with next point layer having 10.000 points:

enter image description here

After running the script 5 times, it can be observed that selected points were always the same:

enter image description here

| improve this answer | |
  • I don't know much about python. Can you please be more specific about how I should incorporate the code above? @xunilk – Z N Aug 21 '17 at 3:28
  • 2
    I answered your question because you tagged it with python. I supposed that you were fairly experienced with language. So, you first need to search how to open QGIS Python Console and load and run a script. Afterward, copy/paste above script in your scripts folder with a suggestive name. You also need to change in registry.mapLayersByName('Points') name 'Points' for your name shapefile. – xunilk Aug 21 '17 at 21:49
  • This worked very well for me! Thanks so much @xunilk for your help – Z N Aug 22 '17 at 4:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.