Random

Random#

class braintools.conn.Random(prob, allow_self_connections=False, weight=None, delay=None, **kwargs)#

Random connectivity with fixed connection probability.

This is the fundamental random connectivity pattern for point neurons, where each potential connection is made with a fixed probability.

Changed in version 0.4.0: Edges are sampled without a dense N_pre x N_post mask: the edge count is drawn from Binomial(M, prob) over the M allowed pairs and that many distinct pairs are drawn uniformly. This is the same distribution as one Bernoulli draw per pair, but the edges a given seed produces differ from earlier versions.

Parameters:
  • prob (float) – Connection probability between 0 and 1.

  • allow_self_connections (bool) – Whether to allow neurons to connect to themselves.

  • weight (Initialization | float | int | ndarray | Array | Quantity | None) –

    Weight initialization. Can be:

    • Initialization class (e.g., Normal, LogNormal, Constant)

    • Scalar value (float/int, stays dimensionless; pass a brainunit Quantity such as 2.5 * u.nS for physical units)

    • Quantity scalar or array

    • Array-like values

    If None, no weights are generated.

  • delay (Initialization | float | int | ndarray | Array | Quantity | None) –

    Delay initialization. Can be:

    • Initialization class (e.g., ConstantDelay, UniformDelay)

    • Scalar value (float/int, stays dimensionless; pass a brainunit Quantity such as 1.0 * u.ms for physical units)

    • Quantity scalar or array

    • Array-like values

    If None, no delays are generated.

  • seed (int, optional) – Random seed for reproducible results.

Notes

  • Self-connections are only removed when allow_self_connections=False and pre and post have the same number of neurons.

  • Time and memory are O(number of edges): 1e5 x 1e5 neurons at prob=0.001 (1e7 edges) build in about a second.

  • Edges are returned sorted by (pre, post).

  • This is host-side NumPy build-time code; it is not traceable by jax.jit, jax.vmap or jax.grad.

Examples

Basic random connectivity:

>>> import brainunit as u
>>> from braintools.conn import Random
>>> from braintools.init import Constant
>>>
>>> # With weights and delays
>>> conn = Random(
...     prob=0.1,
...     weight=Constant(2.0 * u.nS),
...     delay=Constant(1.0 * u.ms),
...     seed=42
... )
>>> result = conn(pre_size=1000, post_size=1000)
>>> result.n_connections
99603
>>>
>>> # Topology only (no weights or delays)
>>> topology_only = Random(prob=0.1, seed=42)
>>> result = topology_only(pre_size=1000, post_size=1000)
>>>
>>> # Using bare scalar values (remain dimensionless)
>>> simple_conn = Random(prob=0.1, weight=2.5, delay=1.0, seed=42)
>>> result = simple_conn(pre_size=1000, post_size=1000)

Random with realistic synaptic weights:

>>> from braintools.init import LogNormal, Normal
>>>
>>> # AMPA-like excitatory synapses
>>> ampa_conn = Random(
...     prob=0.05,
...     weight=LogNormal(mean=1.0 * u.nS, std=0.5 * u.nS),
...     delay=Normal(mean=1.5 * u.ms, std=0.3 * u.ms)
... )

Inhibitory connections with Dale’s principle:

>>> from braintools.init import Normal, Constant
>>>
>>> # GABA-like inhibitory synapses
>>> gaba_conn = Random(
...     prob=0.08,
...     weight=Normal(mean=-0.8 * u.nS, std=0.2 * u.nS),
...     delay=Constant(0.8 * u.ms)
... )
generate(pre_size, post_size, pre_positions=None, post_positions=None, **kwargs)[source]#

Generate random point neuron connections.

Return type:

ConnectionResult