RAND

The RAND function returns a random floating-point value between 0 and 1. An argument can be specified as an optional seed value.

Read syntax diagram
>>-RAND(-+--------------------+-)------------------------------><
         '-numeric-expression-'     

The schema is SYSIBM.

numeric-expression
If numeric-expression is specified, it is used as the seed value. The argument must be an expression that returns a value of a built-in integer data type (SMALLINT or INTEGER). The value must be between 0 and 2,147,483,646.

Start of changeThe argument can also be a character string or graphic string data type. The string input is implicitly cast to a numeric value of DECFLOAT(34) and then assigned to an INTEGER value.End of change

The result of the function is a double precision floating-point number.

The result can be null; if the argument is null, the result is the null value.

A specific seed value, other than zero, 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. The seed value is used only for the first invocation of an instance of the RAND function within a statement. RAND(0) is processed the same as RAND().

The RAND function is a not deterministic.

Example: Assume that host variable HRAND is an INTEGER with a value of 100. The following statement returns a random floating-point number between 0 and 1, such as the approximate value .0121398:
   SELECT RAND(:HRAND)
     FROM SYSIBM.SYSDUMMY1;
To generate values in a numeric interval other than 0 to 1, multiply the RAND function by the size of the interval that you want. 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;