or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

advanced-utilities.mdcombinatorics.mdcombining.mdcomparison.mdgrouping.mdindex.mdindexing.mditeration-utilities.mdlookahead.mdmathematical.mdrandom-operations.mdselecting.mdsequence-utilities.mdspecial-purpose.mdsummarizing.mduniqueness.mdutility-classes.mdwindowing.md

random-operations.mddocs/

0

# Random Operations

1

2

Functions for generating random selections and samples from combinatorial spaces.

3

4

## Capabilities

5

6

### Random Combinatorial Selections

7

8

Functions for generating random elements from combinatorial spaces.

9

10

```python { .api }

11

def random_product(*iterables: Iterable[Any], repeat: int = 1) -> tuple[Any, ...]: ...

12

def random_permutation(iterable: Iterable[Any], r: int = None) -> tuple[Any, ...]: ...

13

def random_combination(iterable: Iterable[Any], r: int) -> tuple[Any, ...]: ...

14

def random_combination_with_replacement(iterable: Iterable[Any], r: int) -> tuple[Any, ...]: ...

15

```

16

17

**Usage:**

18

19

```python

20

from more_itertools import random_product, random_permutation, random_combination

21

22

# Generate random Cartesian product element

23

pools = ['ABC', '123']

24

rand_prod = random_product(*pools) # e.g., ('B', '3')

25

26

# Generate random permutation

27

data = 'ABCDE'

28

rand_perm = random_permutation(data, 3) # e.g., ('D', 'A', 'C')

29

30

# Generate random combination

31

rand_combo = random_combination(data, 3) # e.g., ('A', 'C', 'E')

32

33

# Generate random combination with replacement

34

rand_combo_repl = random_combination_with_replacement(data, 3) # e.g., ('A', 'A', 'D')

35

```

36

37

### Sampling Operations

38

39

Functions for sampling elements from iterables.

40

41

```python { .api }

42

def sample(iterable: Iterable[Any], k: int, *, counts: Iterable[int] = None) -> list[Any]: ...

43

```

44

45

**Usage:**

46

47

```python

48

from more_itertools import sample

49

50

# Sample k elements from iterable

51

data = range(100)

52

sampled = sample(data, 5) # e.g., [23, 7, 45, 12, 89]

53

54

# Sample with weights (counts)

55

items = ['A', 'B', 'C']

56

weights = [1, 3, 2] # B is 3x more likely than A

57

weighted_sample = sample(items, 10, counts=weights)

58

# e.g., ['B', 'C', 'B', 'A', 'B', 'C', 'B', 'B', 'C', 'B']

59

```