API reference
polars-random exposes the same set of distributions through four interchangeable entry points. Pick whichever fits your pipeline; the underlying Rust kernel is the same.
Top-level functions
Returns a pl.Expr by default, or a pl.Series of length size when size= is given.
Uniform [low, high) random draws.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
low
|
float, str (column name), pl.Expr, or None
|
Distribution bounds. Must both be scalars or both be column-like.
Defaults to |
None
|
high
|
float, str (column name), pl.Expr, or None
|
Distribution bounds. Must both be scalars or both be column-like.
Defaults to |
None
|
seed
|
int or None
|
Reproducible draws. |
None
|
size
|
(int or None, keyword - only)
|
If given, eagerly evaluate and return a Series of that length. Otherwise returns a polars Expr to be used in a select/with_columns. |
None
|
Returns:
| Type | Description |
|---|---|
Expr or Series
|
|
Source code in polars_random/__init__.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | |
Normal (Gaussian) random draws.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mean
|
float, str (column name), pl.Expr, or None
|
Distribution parameters. Must both be scalars or both be column-like. |
0.0
|
std
|
float, str (column name), pl.Expr, or None
|
Distribution parameters. Must both be scalars or both be column-like. |
0.0
|
seed
|
int or None
|
|
None
|
size
|
(int or None, keyword - only)
|
If given, eagerly evaluate and return a Series of that length. |
None
|
Returns:
| Type | Description |
|---|---|
Expr or Series
|
|
Source code in polars_random/__init__.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | |
Binomial random draws.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int, str (column name), or pl.Expr
|
Number of trials. |
required |
p
|
float, str (column name), or pl.Expr
|
Probability of success. |
required |
seed
|
int or None
|
|
None
|
size
|
(int or None, keyword - only)
|
If given, eagerly evaluate and return a Series of that length. |
None
|
Returns:
| Type | Description |
|---|---|
Expr or Series
|
|
Source code in polars_random/__init__.py
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | |
Uniform random integers in [low, high).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
low
|
int, str (column name), or pl.Expr
|
Bounds; |
0
|
high
|
int, str (column name), or pl.Expr
|
Bounds; |
0
|
seed
|
int or None
|
|
None
|
size
|
(int or None, keyword - only)
|
If given, eagerly evaluate and return a Series of that length. |
None
|
Returns:
| Type | Description |
|---|---|
Expr or Series
|
|
Source code in polars_random/__init__.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | |
pl.col(...).random — expression namespace
Use inside any expression context (select, with_columns, lazy queries, group-by aggregations, …). The parent expression provides the row count.
import polars as pl
import polars_random # registers the namespace
df.with_columns(noise=pl.col("id").random.normal(mean=0, std=1, seed=42))
Available methods: rand / uniform, normal, binomial, randint. Same parameters as the top-level functions, minus size.
df.random — DataFrame namespace
Namespace for adding columns of random draws to a DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
The dataframe to apply the random functions on. |
required |
Source code in polars_random/__init__.py
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 | |
lf.random — LazyFrame namespace
Same API as df.random but returns a pl.LazyFrame. Lets the random draws stay inside a lazy plan and be optimized alongside the rest of your query.
(
df.lazy()
.filter(pl.col("active"))
.random.normal(seed=42, name="noise")
.collect()
)
Null handling
When a parameter is supplied as a column or expression, any null in that column is propagated to the output as null instead of raising. Scalar parameters are validated up front (seed >= 0, 0 <= p <= 1, valid distribution params) and raise ValueError / PolarsError if invalid.