Yog.FSharp
Getting Started Examples API Reference GitHub

Random Module

Random graph generators for stochastic network models.

Functions and values

Function or value Description

barabasiAlbert n m kind

Full Usage: barabasiAlbert n m kind

Parameters:
Returns: Graph<unit, int>

Barabási-Albert model: creates scale-free networks via preferential attachment.

Generates a random graph with a power-law degree distribution (scale-free). New nodes preferentially attach to existing high-degree nodes ("rich get richer").

Time Complexity: O(n * m * average_degree)

Parameters

  • n: Total number of nodes
  • m: Number of edges each new node creates (must be < n)
  • kind: Directed or Undirected

Example

1: 
2: 
// Scale-free network with 1000 nodes, each connecting to 3 existing nodes
let ba = Random.barabasiAlbert 1000 3 Undirected

Properties

  • Power-law degree distribution: P(k) ~ k^(-3)
  • Hub nodes with very high degree
  • Small-world properties

Use Cases

  • Social network modeling
  • Citation network analysis
  • Web graph simulation
val ba: obj

n : int
m : int
kind : GraphType
Returns: Graph<unit, int>

erdosRenyiGnm n m kind

Full Usage: erdosRenyiGnm n m kind

Parameters:
Returns: Graph<unit, int>

Erdős-Rényi G(n, m) model: exactly m edges are added uniformly at random.

Unlike G(n, p) which includes each edge independently with probability p, G(n, m) guarantees exactly m edges in the resulting graph.

Time Complexity: O(m) expected

Parameters

  • n: Number of nodes
  • m: Exact number of edges to add
  • kind: Directed or Undirected

Example

1: 
2: 
// Random graph with 50 nodes and exactly 100 edges
let graph = Random.erdosRenyiGnm 50 100 Undirected

Properties

  • Exactly m edges (unlike G(n,p) which has expected m edges)
  • Uniform distribution over all graphs with n nodes and m edges

Use Cases

  • Fixed edge count requirements
  • Random graph benchmarking
  • Testing with specific densities
val graph: obj

n : int
m : int
kind : GraphType
Returns: Graph<unit, int>

erdosRenyiGnp n p kind

Full Usage: erdosRenyiGnp n p kind

Parameters:
Returns: Graph<unit, int>

Erdős-Rényi G(n, p) model: each edge exists with probability p.

Generates a random graph where each possible edge is included independently with probability p. For undirected graphs, each unordered pair is considered once.

Time Complexity: O(n²)

Parameters

  • n: Number of nodes
  • p: Edge probability (0.0 to 1.0)
  • kind: Directed or Undirected

Example

1: 
2: 
3: 
4: 
5: 
// Sparse random graph
let sparse = Random.erdosRenyiGnp 100 0.05 Undirected

// Dense random graph
let dense = Random.erdosRenyiGnp 50 0.8 Directed

Properties

  • Expected number of edges: p * n(n-1)/2 (undirected) or p * n(n-1) (directed)
  • Phase transition at p = 1/n (giant component emerges)

Use Cases

  • Random network modeling
  • Percolation studies
  • Average-case algorithm analysis
val sparse: obj
val dense: obj

n : int
p : float
kind : GraphType
Returns: Graph<unit, int>

randomTree n kind

Full Usage: randomTree n kind

Parameters:
Returns: Graph<unit, int>

Generates a uniformly random tree on n nodes.

Creates a tree by starting with node 0 and repeatedly connecting new nodes to random nodes already in the tree. This produces a uniform distribution over all labeled trees.

Time Complexity: O(n²) expected

Example

1: 
2: 
let tree = Random.randomTree 50 Undirected
// Random tree with 50 nodes, 49 edges

Properties

  • Exactly n-1 edges (tree property)
  • Connected
  • Acyclic
  • Uniform distribution over all labeled trees

Use Cases

  • Random spanning tree generation
  • Tree algorithm testing
  • Network topology generation
  • Phylogenetic tree simulation
val tree: obj

n : int
kind : GraphType
Returns: Graph<unit, int>

wattsStrogatz n k p kind

Full Usage: wattsStrogatz n k p kind

Parameters:
Returns: Graph<unit, int>

Watts-Strogatz model: small-world network (ring lattice + rewiring).

Generates a graph with both high clustering (like regular lattices) and short path lengths (like random graphs). Starts with a ring lattice and rewires edges with probability p.

Time Complexity: O(n * k)

Parameters

  • n: Number of nodes (must be >= 3)
  • k: Each node connects to k nearest neighbors (must be even, < n)
  • p: Rewiring probability (0.0 = regular lattice, 1.0 = random)
  • kind: Directed or Undirected

Example

1: 
2: 
// Small-world network: 100 nodes, 6 neighbors each, 10% rewiring
let sw = Random.wattsStrogatz 100 6 0.1 Undirected

Properties

  • High clustering coefficient
  • Short average path length
  • p=0: regular lattice, p=1: random graph

Use Cases

  • Social network modeling (six degrees of separation)
  • Neural network topology
  • Epidemic spread modeling
val sw: obj

n : int
k : int
p : float
kind : GraphType
Returns: Graph<unit, int>