pyspark.sql.functions.randstr#

pyspark.sql.functions.randstr(length, seed=None)[source]#

Returns a string of the specified length whose characters are chosen uniformly at random from the following pool of characters: 0-9, a-z, A-Z. The random seed is optional. The string length must be a constant two-byte or four-byte integer (SMALLINT or INT, respectively).

New in version 4.0.0.

Parameters
lengthColumn or int

Number of characters in the string to generate.

seedColumn or int

Optional random number seed to use.

Returns
Column

The generated random string with the specified length.

Examples

>>> import pyspark.sql.functions as sf
>>> spark.range(0, 10, 1, 1).select(sf.randstr(16, 3)).show()
+----------------+
|  randstr(16, 3)|
+----------------+
|nurJIpH4cmmMnsCG|
|fl9YtT5m01trZtIt|
|PD19rAgscTHS7qQZ|
|2CuAICF5UJOruVv4|
|kNZEs8nDpJEoz3Rl|
|OXiU0KN5eaXfjXFs|
|qfnTM1BZAHtN0gBV|
|1p8XiSKwg33KnRPK|
|od5y5MucayQq1bKK|
|tklYPmKmc5sIppWM|
+----------------+