All Products
Search
Document Center

MaxCompute:RAND

Last Updated:Jul 24, 2023

Returns a random number of the DOUBLE type. The value ranges from 0 to 1.

Syntax

double rand(bigint <seed>)

Parameters

seed: optional. A value of the BIGINT type. This parameter specifies the random seed that determines the starting point in generating random numbers.

Note

You can use seed to determine the random number sequence. After seed is determined, the return value of this function is fixed. If the execution environment is the same and the seed value remains unchanged, the return value is the same. If you need to return different results, you must modify the seed value.

You can add the following configuration before the SQL statement. The default value of the parameter is false.

set odps.sql.executionengine.enable.rand.time.seed=true|false;
  • If you set the odps.sql.executionengine.enable.rand.time.seed parameter to false, the RAND function uses the instance ID as the seed to generate random numbers. This ensures the idempotence of the RAND function.

  • If you set the odps.sql.executionengine.enable.rand.time.seed parameter to true, the RAND function uses the current system time as the seed to generate random numbers. However, the RAND function is no longer idempotent and cannot be used as a shuffle key. In this case, the return value differs each time you execute the RAND function.

Return value

A value of the DOUBLE type is returned.

Examples

-- The return value is 0.7308781907032909. 
select rand(1);

-- If the odps.sql.executionengine.enable.rand.time.seed parameter is set to false, the return value remains unchanged.
set odps.sql.executionengine.enable.rand.time.seed=false;
select rand();
-- The following result is returned:
+------------+
| _c0        |
+------------+
| 4.7147460303803655E-4 |
+------------+

select rand();
-- The following result is returned:
+------------+
| _c0        |
+------------+
| 4.7147460303803655E-4 |
+------------+

-- If the odps.sql.executionengine.enable.rand.time.seed parameter is set to true, the return value differs each time.
set odps.sql.executionengine.enable.rand.time.seed=true;
select rand();
-- The following result is returned:
+------------+
| _c0        |
+------------+
| 0.4111229695431529 |
+------------+
select rand();
-- The following result is returned:
+------------+
| _c0        |
+------------+
| 0.8212525247695169 |
+------------+
Note

The parameter odps.sql.executionengine.enable.rand.time.seed takes effect only if the seed parameter is not specified in the RAND function. If the seed parameter is specified in the RAND function, the value of the odps.sql.executionengine.enable.rand.time.seed parameter is automatically set to false.

Related functions

RAND is a mathematical function. For more information about functions related to data computing and conversion, see Mathematical functions.