random
random defines functions that generate random values for various distributions, it's intended to be a drop-in subset of Python's random module for Starlark.
Functions
randbytes(n)
Generate a random byte string containing n number of bytes.
Parameters
name | type | description |
|---|---|---|
|
| If n bytes is non-positive or not supplied, a reasonable default is used. |
Examples
basic
Generate a random byte string containing 10 bytes.
randstr(chars, n)
Generate a random string containing n number of unicode characters from the given unicode string.
Parameters
name | type | description |
|---|---|---|
|
| The characters to choose from. |
|
| The length of the string. If n is non-positive or not supplied, a reasonable default is used. |
Examples
basic
Generate a random string containing 10 characters from the given unicode string.
randb32(n, sep)
Generate a random base32 string containing n number of bytes with optional separator dash for every sep characters.
Parameters
name | type | description |
|---|---|---|
|
| The number of bytes to generate. If n is non-positive or not supplied, a reasonable default is used. |
|
| The number of characters to separate with a dash, if it's non-positive or not supplied, no separator is used. |
Examples
basic
Generate a random base32 string containing 10 bytes with no separator.
randint(a,b)
Return a random integer N such that a <= N <= b.
Parameters
name | type | description |
|---|---|---|
|
| The lower bound of the range. |
|
| The upper bound of the range. |
Examples
basic
Return a random integer N such that 0 <= N <= 10.
random()
Return a random floating point number in the range 0.0 <= X < 1.0.
Examples
basic
Return a random floating point number in the range [0.0, 1.0).
uniform(a, b)
Return a random floating point number N such that a <= N <= b for a <= b and b <= N <= a for b < a. The end-point value b may or may not be included in the range depending on floating-point rounding in the equation a + (b-a) * random().
Parameters
name | type | description |
|---|---|---|
|
| The lower bound of the range. |
|
| The upper bound of the range. |
Examples
basic
Return a random floating point number N such that 5.0 <= N <= 10.0.
uuid()
Generate a random UUID (RFC 4122 version 4).
Examples
basic
Generate a random UUID.
choice(seq)
Return a random element from the non-empty sequence seq.
Parameters
name | type | description |
|---|---|---|
|
| A non-empty sequence. |
Examples
basic
Return a random element from the non-empty sequence [1, 2, 3, 4, 5].
shuffle(x)
Shuffle the sequence x in place.
Parameters
name | type | description |
|---|---|---|
|
| A non-empty sequence. |
Examples
basic
Shuffle the sequence [1, 2, 3, 4, 5] in place.