Skip to content

Random

Random number generation from mechestim.random. Sampling operations are counted — each sample costs numel(output) FLOPs, and shuffle/permutation costs n * ceil(log2(n)) FLOPs. Only configuration helpers (seed, get_state, set_state, default_rng) are free (0 FLOPs).

mechestim.random

Counted wrappers for numpy.random.

Most samplers deduct numel(output) FLOPs from the active budget. Shuffle-like operations (permutation, shuffle, choice without replacement) deduct n * ceil(log2(n)) FLOPs.

Configuration helpers (seed, get_state, set_state, default_rng, RandomState) are free.

Any attribute not listed here is forwarded to numpy.random via __getattr__ without budget deduction.

bytes(length)

Counted version of numpy.random.bytes.

Cost: length FLOPs.

Source code in src/mechestim/random/__init__.py
def bytes(length):
    """Counted version of ``numpy.random.bytes``.

    Cost: ``length`` FLOPs.
    """
    budget = require_budget()
    cost = _builtins.max(int(length), 1)
    budget.deduct("random.bytes", flop_cost=cost, subscripts=None, shapes=((length,),))
    return _npr.bytes(length)

choice(a, size=None, replace=True, p=None)

Counted version of numpy.random.choice.

Cost: numel(output) FLOPs if replace=True; sort_cost(n) = n * ceil(log2(n)) FLOPs if replace=False.

Source code in src/mechestim/random/__init__.py
def choice(a, size=None, replace=True, p=None):
    """Counted version of ``numpy.random.choice``.

    Cost: numel(output) FLOPs if ``replace=True``;
    sort_cost(n) = n * ceil(log2(n)) FLOPs if ``replace=False``.
    """
    budget = require_budget()
    if isinstance(a, (int, _np.integer)):
        n = int(a)
    else:
        a_arr = _np.asarray(a)
        n = a_arr.shape[0] if a_arr.ndim > 0 else 1
    if replace:
        out_size = _output_size(size=size)
        cost = _builtins.max(out_size, 1)
        budget.deduct(
            "random.choice", flop_cost=cost, subscripts=None, shapes=((out_size,),)
        )
    else:
        cost = sort_cost(n)
        budget.deduct("random.choice", flop_cost=cost, subscripts=None, shapes=((n,),))
    return _npr.choice(a, size=size, replace=replace, p=p)

permutation(x)

Counted version of numpy.random.permutation.

Cost: sort_cost(n) = n * ceil(log2(n)) FLOPs.

Source code in src/mechestim/random/__init__.py
def permutation(x):
    """Counted version of ``numpy.random.permutation``.

    Cost: sort_cost(n) = n * ceil(log2(n)) FLOPs.
    """
    budget = require_budget()
    n = int(x) if isinstance(x, (int, _np.integer)) else x.shape[0]
    cost = sort_cost(n)
    budget.deduct("random.permutation", flop_cost=cost, subscripts=None, shapes=((n,),))
    return _npr.permutation(x)

shuffle(x, axis=0)

Counted version of numpy.random.shuffle.

Modifies x in-place. Cost: sort_cost(n) = n * ceil(log2(n)) FLOPs.

Source code in src/mechestim/random/__init__.py
def shuffle(x, axis=0):
    """Counted version of ``numpy.random.shuffle``.

    Modifies ``x`` in-place. Cost: sort_cost(n) = n * ceil(log2(n)) FLOPs.
    """
    budget = require_budget()
    if hasattr(x, "shape"):
        n = x.shape[axis]
    else:
        n = len(x)
    cost = sort_cost(n)
    budget.deduct("random.shuffle", flop_cost=cost, subscripts=None, shapes=((n,),))
    _npr.shuffle(x)