nwrandom

Generate a random network

Syntax

nwrandom 
nodes
,
[prob(float)
density(float)
census(mutual asym [null])
degree(numlist|varname)
outdegree(numlist|varname) indegree(numlist|varname)
swaps(int)
weights(p1, p2,...)
undirected
directed
ntimes(int)
name(newnetname)
labs(lab1, lab2, ...)
selfloop
xvars]
   
nodes number of nodes
prob(float) probability for a tie
density(float) exact density of the new network
census(mutual [asym null]) dyad census of the new network
degree(numlist|varname) degree of every node of a new undirected network: a list of nodes numbers, or a variable (its first nodes observations)
outdegree() indegree() out-degree and in-degree of every node of a new directed network; both are needed
swaps(int) with degrees: the number of attempted tie swaps that randomize the network; default = 10 times the number of ties
weights(p1, p2,...) probabilities p_k for tie weights k
undirected generate an undirected network; default = directed
directed generate a directed network; this is the default, so the option changes nothing (it cannot be combined with undirected)
ntimes(int) number of random networks to be generated; default = 1
name(newnetname) name of the new random network; default = random
labs(lab1, lab2, …) overwrite node labels
selfloop allow self-loops (a node tied to itself) in the generated network; default = no self-loops
xvars generate Stata variables for the network
noreplace reserved; currently a no-op - the create/replace collision guard on name() already applies regardless

Description

nwrandom generates a (un-)directed, (un-)weighted Erdos-Renyi network. Each potential tie in the network has the same probability to exist, which is defined by prob(). Option prob() generates ties based on probabilities, which means that the exact number of ties can vary.

Alternatively, the overall density of the network can be specified with density(). This option always generates the same number of ties ( = density * nodes), where each tie has the same probability to exist.

Lastly, one can also generate a random network that has a specific dyad census using census() (see nwdyads).

A random network with given degrees is drawn with degree() (undirected) or with outdegree() and indegree() (directed); see Networks with given degrees below.

Either prob(), density(), census() or the degrees need to be specified.

With option weights(p1, p2,…) the command generates a weighted network. Here, p_k stands for the probability to sample tie weight k. The probabilities p1, p2…, pn do not necessarily have to sum up to one; they are standardized. For example, the following produces a random network with 10 nodes, where each tie has the probability 0.5 to exist. Furthermore, each one of these randomly sampled ties gets assigned a tie weight because of option weights(). In this case, weights(0.0, 0.3,0.7) indicates that tie weight 1 should be sampled with probability 0.0, tie weight 2 with probability 0.3 and tie weight 3 with probability 0.7.

. nwrandom 10, prob(.5) weights(0.0, 0.3, 0.7)

The command can also be used to generate many random networks at the same time. For example, the following command produces 100 random networks, where ties have the probability 0.1 to exist.

. nwrandom 50, prob(.1) ntimes(100)

By default, directed networks are generated, option undirected generates undiretced networks instead.

The command can also be used to generate both complete (prob(1)) and empty networks (prob(0)).

Examples

. nwclear
. nwrandom 50, prob(.1)
. nwrandom 15, density(0.5)
. nwrandom 20, prob(.3) ntimes(5)
. nwrandom 10, prob(.2) undirected
. nwrandom 200, census(100 2000)
. nwrandom 10, density(.2) weights(0.1, 0.3, 0.6)
. nwrandom 8, degree(3 3 2 2 2 2 1 1)

Supported network types

Binary: yes (only structural tie placement - see Weighted). Directed: yes, via undirected (default is directed); with degrees, degree() draws an undirected network and outdegree() indegree() a directed one. Weighted: yes, via weights() - a Stata expression assigning each placed tie’s value, independent of the placement mechanism itself (density()/prob()/census()). Signed: not checked. Two-mode: not applicable - this generator always produces a one-mode network.

Stored results

  • nwrandom stores the following in r():

Macros

  • r(netlist) list of new networks

See also


nwcommands version 3.17.6, 9 November 2026 (check your installed version)


Back to top

nwcommands is free software under the GNU General Public License, version 3, as are the model packages nwsaom, nwergm, nwdynam and nwrem. Citing and licenses · Get help · GitHub. Issues with the core go to the nwcommands repository; issues with a model package go to that package's own repository.

This site uses Just the Docs, a documentation theme for Jekyll.