Returns a pseudo-random DOUBLE value in the range [0, 1).
Syntax
double rand(bigint <seed>)
Parameters
seed: Optional. A BIGINT value that sets the starting point for random number generation. When a seed is specified, the function produces a deterministic sequence — the same seed always yields the same result in the same execution environment. To get a different result, change the seed value.
The odps.sql.executionengine.enable.rand.time.seed flag has no effect when seed is specified. Specifying a seed implicitly sets the flag to false.
Return value
DOUBLE
Usage notes
Controlling determinism without a seed
When seed is not specified, the odps.sql.executionengine.enable.rand.time.seed flag controls whether RAND is deterministic. Set the flag before your SQL statement. The default value is false.
set odps.sql.executionengine.enable.rand.time.seed=true|false;
| Flag value | Seed source | Behavior |
|---|---|---|
false (default) |
Instance ID | RAND is idempotent — the same statement returns the same value each time it runs. |
true |
Current system time | RAND is non-deterministic — the return value differs each time you run the statement. RAND cannot be used as a shuffle key in this mode. |
Examples
Example 1: Fixed seed
-- Returns 0.7308781907032909
SELECT rand(1);
Example 2: Idempotent (flag set to false)
Each run of the same statement returns the same value:
SET odps.sql.executionengine.enable.rand.time.seed=false;
SELECT rand();
-- Returns:
-- +------------------------+
-- | _c0 |
-- +------------------------+
-- | 4.7147460303803655E-4 |
-- +------------------------+
SELECT rand();
-- Returns:
-- +------------------------+
-- | _c0 |
-- +------------------------+
-- | 4.7147460303803655E-4 |
-- +------------------------+
Example 3: Non-deterministic (flag set to true)
Each run returns a different value:
SET odps.sql.executionengine.enable.rand.time.seed=true;
SELECT rand();
-- Returns:
-- +--------------------+
-- | _c0 |
-- +--------------------+
-- | 0.4111229695431529 |
-- +--------------------+
SELECT rand();
-- Returns:
-- +--------------------+
-- | _c0 |
-- +--------------------+
-- | 0.8212525247695169 |
-- +--------------------+
What's next
RAND is a mathematical function. For other functions related to data computing and conversion, see Mathematical functions.