---
title: randstr
description: "randstr function in PySpark: 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."
canonical_url: https://docs.databricks.com/aws/en/pyspark/reference/functions/randstr
---

# randstr

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).

*Added in Databricks Runtime 16.1*

For the corresponding Databricks SQL function, see [`randstr` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/randstr.md).

## Syntax

```python
from pyspark.sql import functions as dbf

dbf.randstr(length=<length>, seed=<seed>)
```

## Parameters

| Parameter | Type                          | Description                                     |
| --------- | ----------------------------- | ----------------------------------------------- |
| `length`  | `pyspark.sql.Column` or `int` | Number of characters in the string to generate. |
| `seed`    | `pyspark.sql.Column` or `int` | Optional random number seed to use.             |

## Returns

`pyspark.sql.Column`: The generated random string with the specified length.

## Examples

```python
from pyspark.sql import functions as dbf
spark.range(0, 10, 1, 1).select(dbf.randstr(16, 3)).show()
```
