Random value calculation in QGIS

Is there a way to do a random value calculation within a range in QGIS? I have been able to do this within ArcMap Field Calculator with the following code:

``````Dim max, min
max=20
min=5
x=(Int((max-min+1)*Rnd+min))

__esri_field_calculator_splitter__
x
``````

Thanks

• The built in field calculator doesn't have a "Random" function. This would be relatively easy to do with Python if there's an Advanced Field Calculator plugin (or equivalent) out there somewhere. Anyone? – GIS-Jonathan Apr 26 '12 at 13:47
• That is my thoughts. I do the same thing in the ArcMap Field Calculator with the Advanced option. Is there any development for the Field Cal in QGIS in the next release? – Ryan Garnett Apr 27 '12 at 13:47
• You'd have to ask on the QGIS mailing list - lists.osgeo.org/mailman/listinfo/qgis-user – GIS-Jonathan Apr 27 '12 at 13:57

While QGIS doesn't appear to have this functionality, you may be able to work "around" it.

If you export your data to a SpatiaLite database you can then use the `random()` from SQLite in an SQL statement. I guess something like this:

`update TABLE set COLUMN = ROUND((20-5+1) * random() + 5)`

Any other database would do too, but that one can be treated as a local file and doesn't require installation of something seperate.

QGIS 2.4+ has a couple of random number functions:

``````rand(min,max) # returns a random integer between min and max, inclusive

randf(min,max) # returns a float value.
``````
• Yep, just thought I'd add this in case anyone comes searching! – Simbamangu Jan 31 '15 at 5:42

You could load the DBF into OO/Libre Office Calc and do this there. I think you need to have Base installed for Calc to be able to work with DBFs and you'd need to set the headers for columns that you create appropriately for data-type, field width, precision and so on. I quite often work on shapefile DBFs in Calc and have never had problems doing this, it's very convenient.