I have a shape layer in qgis with blank attribute table. I want to fill attribute table with false data randomly. I successfully update the field with integer values using 'rand' expression. I am stuck at 'how to randomly fill string values.'

Suppose I have shape with drain 500 features. I want to enter values in drain_name field with values "ravi", "Sangpo","ganga",... etc.

Which expression should i use to update the drain name with random string value.

4 Answers 4


You may create your own custom python function through field calculator function editor to create random strings to populate a field:

  1. Click on the function editor tab within field cal

  2. Create new file and copy in code below

  3. Load the function

    from qgis.core import *
    from qgis.gui import *
    import random, string
    @qgsfunction(args='auto', group='Custom')
    def myFun(value1, feature, parent):
        return randomword(10)
    def randomword(length):
        return ''.join(random.choice(string.lowercase) for i in range(length))

Make a call under expression tab like and click Ok button to run:

  • Hi, I am getting Null value after running it.
    – Sachin
    Dec 7, 2016 at 6:02

You can generate true random strings with this expression:


Note that this will not generate random words! Just random characters concatenated.

How it works: generate_series(1,10) creates an array of length 10. This will be the length of your final random string. Change the 10 to whatever string length you wish to generate. Now we loop through this array using array_foreach() and replace every element with a random character. We get this random character using char() function: char(97,122) will create a random character from the unicode table with codes between 97 and 122, which is the latin lowercase alphabet. Lookup here https://en.wikipedia.org/wiki/List_of_Unicode_characters. Change these numbers to whatever characters you wish to generate. As last step, we concatenate the array of random characters to a string using nothing as separator ('').

If you also want a random string length, you can modifiy the expression a little and use rand(min,max) as maximum in generate_series():


Example result:

enter image description here


You can get true random words by using Wordnik API.

First you need to add this custom function:

from qgis.core import *
from qgis.gui import *
import urllib
import json
import xmltodict

@qgsfunction(args='auto', group='Custom', referenced_columns=[])
def json_from_url(url, feature, parent):
    request = urllib.request.Request(url)
    response = urllib.request.urlopen(request)
    response_data = response.read()
    try: # handle JSON response
        encoding = response.info().get_content_charset('utf-8')
        data = json.loads(response_data.decode(encoding))
        data_string = json.dumps(data) # make it a string
    except: # handle XML response
        data = xmltodict.parse(response_data)
        data_string = json.dumps(data) # make it a string

    return data_string

It allows to receive JSON-Responses from online APIs. Then you can use this expression:


Simply adjust the request URL to your needs. Especially replace the Demo-API-Key with your personal one, or you will frequently run out of responses.

Example result:

enter image description here


Create a temporary attribute temp with rand( 1, 3). The higher number here has to be the number of the random values you want to use.

After that, fill the values in drain_name like this:

WHEN "temp" = 1 THEN 'ravi'
WHEN "temp" = 2 THEN 'Sangpo'
WHEN "temp" = 3 THEN 'ganga'

After that you can delete temp again.

  • This is not a random string, you have to write them. And it's cumbersome to write 'words', especially if you have 500 features like he said. The other solution is much better.
    – etrimaille
    Aug 10, 2016 at 19:19
  • I know what you mean, but my answer is supposed to show what you need to do in order to get "randomized words" instead of just scrambled up letters. Of course, that way you have to type them out first (or better, use a word list and some find/replace-magic).
    – karpfen
    Aug 10, 2016 at 19:29

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.