RANDOM or RAND

The Start of changeRANDOM orEnd of change RAND function returns a floating point value greater than or equal to 0 and less than or equal to 1.

Read syntax diagramSkip visual syntax diagramRANDOMRAND (expression)
expression
If an expression is specified, it is used as the seed value. The argument must be an expression that returns a value of a built-in small integer, large integer, character-string, or graphic-string data type. A string argument is cast to integer before evaluating the function. For more information on converting strings to integer, see INTEGER or INT.

The data type of the result is double-precision floating point. If the argument can be null, the result can be null; if the argument is null, the result is the null value.

A specific seed value will produce the same sequence of random numbers for a specific instance of a RAND function in a query each time the query is executed. If a seed value is not specified, a different sequence of random numbers is produced each time the query is executed.

The seed value is used only for the first invocation of an instance of the RAND function within a statement.

RAND is a non-deterministic function.

Example

  • Assume that host variable HRAND is an INTEGER with a value of 100. The following statement:
      SELECT RAND(:HRAND)
        FROM SYSIBM.SYSDUMMY1
    Returns a random floating-point number between 0 and 1, such as the approximate value .0121398.
  • To generate values in a numeric interval other than 0 to 1, multiply the RAND function by the size of the wanted interval. For example, to get a random number between 0 and 10, such as the approximate value 5.8731398, multiply the function by 10:
      SELECT RAND(:HRAND) * 10
        FROM SYSIBM.SYSDUMMY1