Skip to main content

rand

Generates a random column with independent and identically distributed (i.i.d.) samples uniformly distributed in [0.0, 1.0). Supports Spark Connect.

The function is non-deterministic in general case.

For the corresponding Databricks SQL function, see rand function.

Syntax

Python
from pyspark.databricks.sql import functions as dbf

dbf.rand(seed=<seed>)

Parameters

Parameter

Type

Description

seed

int, optional

Seed value for the random generator.

Returns

pyspark.sql.Column: A column of random values.

Examples

Python
from pyspark.databricks.sql import functions as dbf
spark.range(0, 2, 1, 1).select("*", dbf.rand()).show() # doctest: +SKIP
Output
+---+-------------------------+
| id|rand(-158884697681280011)|
+---+-------------------------+
| 0| 0.9253464547887...|
| 1| 0.6533254118758...|
+---+-------------------------+

Python
from pyspark.databricks.sql import functions as dbf
spark.range(0, 2, 1, 1).select("*", dbf.rand(seed=42)).show() # doctest: +SKIP
Output
+---+------------------+
| id| rand(42)|
+---+------------------+
| 0| 0.619189370225...|
| 1|0.5096018842446...|
+---+------------------+