Bug report #11231

Function rand() only gives values up to ~32000

Added by Matthias Kuhn about 10 years ago. Updated over 5 years ago.

Status:Closed
Priority:Normal
Assignee:-
Category:Expressions
Affected QGIS version:master Regression?:No
Operating System:Windows 7, 64 bit Easy fix?:No
Pull Request or Patch supplied:No Resolution:end of life
Crashes QGIS or corrupts data:No Copied to github as #:19539

Description

On a spatialite layer, creating a new field with the field calculator and the expression "rand( 100000, 5000000 )" gives values of 100000 - 140000.

This seems not to be a problem on linux.

"randf()" also works properly.

The data type of the field was Integer.

QGIS was installed as 32bit.

History

#1 Updated by Giovanni Manghi about 10 years ago

  • Affected QGIS version changed from 2.4.0 to master

confirmed on the latest master on Windows (qgis 32 and 64 bit). Ok on Linux.

#2 Updated by Matthias Kuhn about 10 years ago

Investigating a bit:

Visual Studio (up to 2013) has RAND_MAX defined as 0x7fff: 32767. Author's note: which seems quite stupid on a 32bit/64bit system.
(http://msdn.microsoft.com/en-us/library/2dfe3bzd.aspx)

A possibility to overcome this is to call rand() twice and bitshift/combine the values:
http://stackoverflow.com/questions/4832975/win32-higher-random-max-value

Or to use the <random> header file which is part of C++ 2011 and boost
http://msdn.microsoft.com/en-us/library/bb982398.aspx

#3 Updated by Giovanni Manghi over 7 years ago

  • Easy fix? set to No
  • Regression? set to No

#4 Updated by Giovanni Manghi over 5 years ago

  • Resolution set to end of life
  • Status changed from Open to Closed

Also available in: Atom PDF