RandomGraph[{n,m}]
给出具有 n 个顶点和 m 条边的伪随机图.
RandomGraph[{n,m},k]
给出 k 个伪随机图组成的列表.
RandomGraph[gdist,…]
来自随机图分布 gdist 的样本.
RandomGraph
图分布列表 »RandomGraph[{n,m}]
给出具有 n 个顶点和 m 条边的伪随机图.
RandomGraph[{n,m},k]
给出 k 个伪随机图组成的列表.
RandomGraph[gdist,…]
来自随机图分布 gdist 的样本.
更多信息和选项
- RandomGraph[{n,m}] 等同于 RandomGraph[UniformGraphDistribution[n,m]].
- 当运行 Wolfram 语言时,RandomGraph 给出不同的伪随机图序列. 通过使用 SeedRandom,您可以获得重复序列.
- 顶点采用整数 1、…、n.
- SeedRandom 的一个 method(方法)选项可以用来指定所用的伪随机生成器.
- RandomGraph[dist,{n1,n2,…}] 给出图的一个 n1×n2×… 数组.
- RandomGraph 有与 Graph 同样的选项.
所有选项的列表
范例
打开所有单元 关闭所有单元基本范例 (2)
RandomGraph[{5, 6}]RandomGraph[BernoulliGraphDistribution[6, 0.4]]data = EdgeCount /@ RandomGraph[BernoulliGraphDistribution[6, 0.4], 10 ^ 3];Show[Histogram[data, Automatic, "PDF"],
DiscretePlot[PDF[BinomialDistribution[15, 0.4], m], {m, 0, 15}, Joined -> True]]范围 (12)
RandomGraph[{5, 6}]RandomGraph[{6, 7}, DirectedEdges -> True]RandomGraph[BernoulliGraphDistribution[8, 0.3], 4]RandomGraph[BernoulliGraphDistribution[8, 0.3], {2, 3}]RandomGraph[BarabasiAlbertGraphDistribution[15, 2], 4]RandomGraph[BernoulliGraphDistribution[8, 0.4], 4]RandomGraph[UniformGraphDistribution[8, 12], 4]RandomGraph[DegreeGraphDistribution[{3, 3, 3, 3, 3, 3, 3, 3}], 4]RandomGraph[SpatialGraphDistribution[15, 0.35], 4]RandomGraph[PriceGraphDistribution[15, 2, 1], 4]RandomGraph[WattsStrogatzGraphDistribution[15, 0.2], 4]g = RandomGraph[{10^4, 10^4}];{VertexCount[g], EdgeCount[g]}选项 (70)
DirectedEdges (1)
RandomGraph[{4, 5}]使用 DirectedEdges->True 来生成一个有向图:
RandomGraph[{4, 5}, DirectedEdges -> True]EdgeLabels (6)
RandomGraph[{3, 3}, EdgeLabels -> {12 -> "Hello"}]el = EdgeList[RandomGraph[{3, 3}]]RandomGraph[{3, 3}, EdgeLabels -> Table[el[[i]] -> Subscript["e", i], {i, Length[el]}]]使用具有符号位置信息的 Placed 来控制边上标签的位置:
Table[RandomGraph[{3, 3}, EdgeLabels -> {12 -> Placed["■■■", p]}, PlotLabel -> p], {p, {"Start", "Middle", "End"}}]Table[RandomGraph[{3, 3}, EdgeLabels -> {12 -> Placed["■■■", p]}, PlotLabel -> p, BaselinePosition -> Bottom], {p, {0, 1 / 4, 1 / 3}}]Table[RandomGraph[{3, 3}, EdgeLabels -> {12 -> Placed["■■■", {1 / 2, p}]}, PlotLabel -> p, BaselinePosition -> Bottom], {p, {{0, 0}, {1 / 2, 1 / 2}, {1, 1}}}]RandomGraph[{3, 3}, EdgeLabels -> {31 -> Placed[{"lbl1", "lbl2"}, {"Start", "End"}]}]RandomGraph[{3, 3}, EdgeLabels -> {31 -> Placed[{"lbl1", "lbl2", "lbl3"}, {"Start", "Middle", "End"}]}]通过 Tooltip 和 StatusArea 的值,使用自动标签:
RandomGraph[{3, 3}, EdgeLabels -> Placed["Name", Tooltip]]RandomGraph[{3, 3}, EdgeLabels -> Placed["Name", StatusArea]]EdgeShapeFunction (6)
获取 EdgeShapeFunction 的一个内置设置列表:
ResourceData["EdgeShapeFunction"]RandomGraph[{3, 3}, EdgeShapeFunction -> "Line"]Table[RandomGraph[{3, 3}, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> ef], {ef, {"BoxLine", "DiamondLine", "DotLine"}}]Table[RandomGraph[{3, 3}, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> ef], {ef, ResourceData["EdgeShapeFunction", "FilledArrow"]}]Table[RandomGraph[{3, 3}, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> ef], {ef, ResourceData["EdgeShapeFunction", "UnfilledArrow"]}]Table[RandomGraph[{3, 3}, EdgeShapeFunction -> {{ef, "ArrowSize" -> 0.1}}, PlotLabel -> ef], {ef, ResourceData["EdgeShapeFunction", "CarvedArrow"]}]RandomGraph[{3, 3}, EdgeShapeFunction -> {12 -> "DotLine"}]RandomGraph[{3, 3}, EdgeShapeFunction -> {12 -> "BoxLine", "DotLine"}]ef[pts_List, e_] :=
Block[{s = 0.015, g = [image]}, {Arrowheads[{{s, 0.33, g}, {s, 0.67, g}}], Arrow[pts]}]RandomGraph[{3, 3}, EdgeShapeFunction -> ef]EdgeShapeFunction 可以与 EdgeStyle 合起来使用:
RandomGraph[{3, 3}, EdgeStyle -> Blue, EdgeShapeFunction -> (Line[#1]&)]EdgeShapeFunction 具有比 EdgeStyle 更高的优先级:
RandomGraph[{3, 3}, EdgeStyle -> Blue, EdgeShapeFunction -> ({Red, Line[#1]}&)]EdgeStyle (2)
GraphHighlight (3)
RandomGraph[{3, 3}, VertexSize -> Tiny, GraphHighlight -> {1}]RandomGraph[{3, 3}, VertexSize -> Tiny, GraphHighlight -> {23}]RandomGraph[{3, 3}, VertexSize -> Tiny, GraphHighlight -> {1, 2, 12, 13}]GraphHighlightStyle (2)
获得 GraphHighlightStyle 的内置设定列表:
ResourceData["GraphHighlightStyle"]使用 GraphHighlightStyle 的内置设定:
RandomGraph[{3, 3}, GraphHighlight -> {1, 23}, VertexSize -> Small, GraphHighlightStyle -> #, PlotLabel -> #]& /@ Select[ResourceData["GraphHighlightStyle"], # =!= Automatic&]GraphLayout (4)
RandomGraph[{3, 3}, GraphLayout -> Automatic]Table[RandomGraph[{10, 20}, GraphLayout -> l, PlotLabel -> l], {l, {"CircularEmbedding", "SpiralEmbedding"}}]Table[RandomGraph[{10, 20}, GraphLayout -> l, PlotLabel -> l], {l, {"SpringEmbedding", "SpringElectricalEmbedding", "HighDimensionalEmbedding"}}]使用 AbsoluteOptions 提取使用布局算法的 VertexCoordinates 计算:
RandomGraph[{3, 3}]AbsoluteOptions[%, VertexCoordinates]PlotTheme (4)
基本主题 (2)
VertexCoordinates (3)
RandomGraph[{3, 3}]使用 AbsoluteOptions 提取所得顶点坐标:
AbsoluteOptions[%, VertexCoordinates]ellipseLayout[n_, {a_, b_}] := Table[{a Cos[2Pi / n u], b Sin[2Pi / n u]}, {u, 1, n}]Graphics[Point[ellipseLayout[20, {2, 1}]]]RandomGraph[{20, 25}, VertexCoordinates -> ellipseLayout[20, {2, 1}]]VertexCoordinates 具有比 GraphLayout 更高的优先级:
RandomGraph[{3, 3}, VertexCoordinates -> Table[{i, i}, {i, 3}], GraphLayout -> "CircularEmbedding"]VertexLabels (13)
RandomGraph[{3, 3}, VertexLabels -> "Name"]RandomGraph[{3, 3}, VertexLabels -> {3 -> "one"}]RandomGraph[{3, 3}, VertexLabels -> Table[i -> Subscript[v, i], {i, 3}]]RandomGraph[{3, 3}, VertexLabels -> {1 -> [image], 2 -> [image], 3 -> [image]}, ImagePadding -> 30]使用具有符号位置信息的 Placed 来控制标签位置,包括内部位置:
Table[RandomGraph[{3, 3}, VertexSize -> 0.1, VertexShapeFunction -> "Square", VertexLabels -> Table[i -> Placed["■■■", p], {i, 3}], PlotLabel -> p, ImagePadding -> 20], {p, {Before, After, Below, Above}}]pl = {{Before, Below}, {After, Below}, {Before, Above}, {After, Above}};Table[RandomGraph[{3, 3}, VertexSize -> 0.1, VertexShapeFunction -> "Square", ImagePadding -> 20, VertexLabels -> Table[i -> Placed["■■■", p], {i, 3}], PlotLabel -> p], {p, pl}]Table[RandomGraph[{3, 3}, VertexSize -> 0.6, VertexLabels -> Table[i -> Placed["■■■", p], {i, 3}], VertexShapeFunction -> "Square", PlotLabel -> p], {p, {Left, Top, Right, Bottom}}]pl = {{Left, Bottom}, {Right, Bottom}, {Left, Top}, {Right, Top}};Table[RandomGraph[{3, 3}, VertexSize -> 0.6, VertexShapeFunction -> "Square", VertexLabels -> Table[i -> Placed["■■■", p], {i, 3}], PlotLabel -> p], {p, pl}]Table[RandomGraph[{3, 3}, VertexSize -> 0.6, ImagePadding -> 10, VertexShapeFunction -> "Square", VertexLabels -> Table[i -> Placed[[image], p], {i, 3}], PlotLabel -> p, BaselinePosition -> Bottom], {p, {{0, 0}, {1 / 2, 1 / 2}, {1, 1}}}]Table[RandomGraph[{3, 3}, VertexSize -> 0.6, VertexShapeFunction -> "Square", ImagePadding -> 20, VertexLabels -> Table[i -> Placed[[image], {{1, 1}, p}], {i, 3}], PlotLabel -> p, BaselinePosition -> Bottom], {p, {{0, 0}, {1 / 2, 1 / 2}, {1, 1}}}]RandomGraph[{3, 3}, VertexLabels -> {1 -> Placed[{"lbl1", "lbl2"}, {Above, Below}]}]RandomGraph[{3, 3}, VertexLabels -> {1 -> Placed[{"lbl1", "lbl2", "lbl3", "lbl4"}, {Above, After, Below, Before}]}]使用 Placed 的变量来控制包括 Tooltip 的格式:
RandomGraph[{3, 3}, VertexLabels -> Placed["Name", Tooltip]]或 StatusArea:
RandomGraph[{3, 3}, VertexLabels -> Placed["Name", StatusArea]]rotateLabel[lab_] := Rotate[lab, 45Degree]RandomGraph[{3, 3}, VertexLabels -> Table[i -> Placed["xxx", Below, rotateLabel], {i, 3}]]panelLabel[lab_] := Panel[lab, FrameMargins -> 0, Background -> Lighter[Yellow, 0.7]]RandomGraph[{3, 3}, VertexLabels -> Table[i -> Placed["xxx", Center, panelLabel], {i, 3}]]hyperlinkLabel[lab_] := Hyperlink[lab, "http://www.wolfram.com"]RandomGraph[{3, 3}, VertexLabels -> Table[i -> Placed["xxx", Center, hyperlinkLabel], {i, 3}]]VertexShape (5)
使用任意 Graphics、Image 或者 Graphics3D 作为一个顶点形状:
Table[RandomGraph[{3, 3}, VertexShape -> s, VertexSize -> Medium], {s, {[image], [image], [image]}}]RandomGraph[{3, 3}, VertexShape -> {2 -> [image]}, VertexSize -> Medium]VertexShape 可以与 VertexSize 合起来使用:
Table[RandomGraph[{3, 3}, VertexSize -> s, VertexShape -> [image]], {s, {Small, Large}}]VertexShape 不受 VertexStyle 影响:
RandomGraph[{3, 3}, VertexSize -> 0.2, VertexShape -> [image], VertexStyle -> Blue]VertexShapeFunction 具有比 VertexShape 更高的优先级:
RandomGraph[{3, 3}, VertexSize -> 0.1, VertexShapeFunction -> "Square", VertexShape -> [image]]VertexShapeFunction (9)
获取 VertexShapeFunction 的内置设置列表:
ResourceData["VertexShapeFunction"]在 "Basic" 集中使用 VertexShapeFunction 的内置设置:
ResourceData["VertexShapeFunction", "Basic"]Table[RandomGraph[{3, 3}, VertexShapeFunction -> vf, VertexSize -> 0.2, PlotLabel -> vf], {vf, {"Triangle", "Square", "Rectangle", "Pentagon", "Hexagon", "Octagon"}}]Table[RandomGraph[{3, 3}, VertexShapeFunction -> vf, VertexSize -> 0.2, PlotLabel -> vf], {vf, {"DownTrapezoid", "UpTrapezoid", "Parallelogram", "FiveDown", "Circle", "Diamond", "Star", "Capsule"}}]在 "Rounded" 集中使用 VertexShapeFunction 的内置设定:
ResourceData["VertexShapeFunction", "Rounded"]Table[RandomGraph[{3, 3}, VertexShapeFunction -> vf, VertexSize -> 0.2, PlotLabel -> vf], {vf, ResourceData["VertexShapeFunction", "Rounded"]}]在 "Concave" 集中,使用 VertexShapeFunction 内置设定:
ResourceData["VertexShapeFunction", "Concave"]Table[RandomGraph[{3, 3}, VertexShapeFunction -> vf, VertexSize -> 0.2, PlotLabel -> vf], {vf, ResourceData["VertexShapeFunction", "Concave"]}]RandomGraph[{3, 3}, VertexShapeFunction -> { 1 -> "Square"}, VertexSize -> 0.2]RandomGraph[{3, 3}, VertexShapeFunction -> { 1 -> "Square", "Triangle"}, VertexSize -> 0.2]vf[{xc_, yc_}, name_, {w_, h_}] :=
Block[{xmin = xc - w, xmax = xc + w, ymin = yc - h, ymax = yc + h},
Polygon[{{xmin, ymin}, {xmax, ymax}, {xmin, ymax}, {xmax, ymin}}]
];RandomGraph[{3, 3}, VertexShapeFunction -> vf, VertexSize -> 0.1]VertexShapeFunction 可以与 VertexStyle 合起来使用:
vf1[{xc_, yc_}, name_, {w_, h_}] := Rectangle[{xc - w, yc - h}, {xc + w, yc + h}]RandomGraph[{3, 3}, VertexSize -> 0.1, VertexStyle -> Blue, VertexShapeFunction -> vf1]VertexShapeFunction 具有比 VertexStyle 更高的优先级:
vf2[{xc_, yc_}, name_, {w_, h_}] := {Red, Rectangle[{xc - w, yc - h}, {xc + w, yc + h}]}RandomGraph[{3, 3}, VertexSize -> 0.1, VertexStyle -> Blue, VertexShapeFunction -> vf2]VertexShapeFunction 可以与 VertexShape 合起来使用:
RandomGraph[{3, 3}, VertexSize -> 0.3, VertexShape -> {1 -> [image]}]VertexShapeFunction 具有比 VertexShape 更高的优先级:
RandomGraph[{3, 3}, VertexSize -> 0.3, VertexShapeFunction -> "Star", VertexShape -> [image]]VertexSize (8)
RandomGraph[{3, 3}, VertexSize -> Automatic]Table[RandomGraph[{3, 3}, VertexSize -> s, PlotLabel -> s], {s, {Tiny, Small, Medium, Large}}]Table[RandomGraph[{3, 3}, VertexSize -> s, PlotLabel -> s], {s, 0.1, 1, 0.3}]Table[RandomGraph[{3, 3}, VertexSize -> {"Scaled", s}, PlotLabel -> {"Scaled", s}], {s, 0.1, 1, 0.3}]Table[RandomGraph[{3, 3}, VertexSize -> s, PlotLabel -> s], {s, {{0.1, 0.2}, {0.2, 0.1}}}]RandomGraph[{3, 3}, VertexSize -> {1 -> 0.2, 2 -> 0.3}]VertexSize 可以与 VertexShapeFunction 合起来使用:
Table[RandomGraph[{3, 3}, VertexSize -> s, VertexShapeFunction -> "Square", PlotLabel -> s], {s, {0.05, 0.1, 0.2}}]VertexSize 可以与 VertexShape 合起来使用:
Table[RandomGraph[{3, 3}, VertexSize -> s, VertexShape -> [image], PlotLabel -> s], {s, {0.1, 0.2, 0.4}}]VertexStyle (4)
Table[RandomGraph[{3, 3}, VertexStyle -> style, VertexSize -> 0.3, PlotLabel -> style], {style, {Yellow, EdgeForm[Dashed]}}]RandomGraph[{3, 3}, VertexStyle -> {1 -> Blue, 2 -> Red}, VertexSize -> Small]VertexShapeFunction 可以与 VertexStyle 合起来使用:
vf1[{xc_, yc_}, name_, {w_, h_}] := Rectangle[{xc - w, yc - h}, {xc + w, yc + h}]RandomGraph[{3, 3}, VertexSize -> 0.1, VertexStyle -> Blue, VertexShapeFunction -> vf1]VertexShapeFunction 具有比 VertexStyle 更高的优先级:
vf2[{xc_, yc_}, name_, {w_, h_}] := {Red, Rectangle[{xc - w, yc - h}, {xc + w, yc + h}]}RandomGraph[{3, 3}, VertexSize -> 0.1, VertexStyle -> Blue, VertexShapeFunction -> vf2]VertexShape 不受 VertexStyle 影响:
RandomGraph[{3, 3}, VertexStyle -> LightBlue, BaseStyle -> EdgeForm[Dotted], VertexSize -> 0.2]VertexStyle 具有比 BaseStyle 更高的优先级:
RandomGraph[{3, 3}, VertexStyle -> LightBlue, BaseStyle -> Gray, VertexSize -> 0.2]应用 (3)
对图分布属性生成随机数据,并且比较它的直方图和概率密度函数:
data = EdgeCount /@ RandomGraph[BernoulliGraphDistribution[10, 0.7], 10 ^ 3];𝒟 = GraphPropertyDistribution[EdgeCount[g], gBernoulliGraphDistribution[10, 0.7]];Show[Histogram[data, Automatic, "ProbabilityDensity"], DiscretePlot[PDF[𝒟, k], {k, 20, 40}, PlotStyle -> PointSize[Medium]]]data = VertexDegree[#, 1]& /@ RandomGraph[BernoulliGraphDistribution[15, 0.7], 10 ^ 3];EstimatedDistribution[data, BinomialDistribution[n, p]]Show[Histogram[data, Automatic, "ProbabilityDensity"], DiscretePlot[PDF[%, k], {k, 0, 15}, PlotStyle -> PointSize[Medium]]]𝒢 = DegreeGraphDistribution[{2, 4, 4, 4, 3, 3, 2}];RandomGraph[𝒢, 4]属性和关系 (10)
RandomGraph 使用整数顶点:
g = RandomGraph[{7, 10}, VertexLabels -> "Name"]VertexList[g]使用 VertexReplace 替换图中的顶点:
VertexReplace[g, {1 -> Subscript[v, 1], 2 -> Subscript[v, 2], 3 -> Subscript[v, 3], 4 -> Subscript[v, 4], 5 -> Subscript[v, 5], 6 -> Subscript[v, 6], 7 -> Subscript[v, 7]}]VertexList[%]使用 SeedRandom 获取可重复随机图:
{RandomGraph[{6, 5}], RandomGraph[{6, 5}]}{SeedRandom[1];RandomGraph[{6, 5}], SeedRandom[1];RandomGraph[{6, 5}]}使用 BlockRandom 阻止对 RandomGraph 的使用影响其他:
{BlockRandom[RandomGraph[{6, 5}]], RandomGraph[{6, 5}]}RandomVariate 从概率分布生成变量:
RandomVariate[NormalDistribution[]]RandomInteger 生成均匀离散随机变量:
RandomInteger[{0, 10}, 10]RandomReal 生成均匀连续变量:
RandomReal[{0, 10}, 10]RandomChoice 从列表生成可重复的随机选择:
RandomChoice[Range[10], 10]RandomSample 从列表生成不重复的随机选择:
RandomSample[Range[20], 10]RandomPrime 生成一个随机质数:
RandomPrime[{1, 100}, 10]RandomImage 生成一个随机图像:
RandomImage[LaplaceDistribution[0, .2], {100, 100}]可能存在的问题 (1)
RandomGraph[{5, 11}]RandomGraph[{n,Binomial[n,2]}] 总是给出具有 n 个顶点的完全图:
RandomGraph[{5, 10}, 3]相关链接
文本
Wolfram Research (2010),RandomGraph,Wolfram 语言函数,https://reference.wolfram.com/language/ref/RandomGraph.html.
CMS
Wolfram 语言. 2010. "RandomGraph." Wolfram 语言与系统参考资料中心. Wolfram Research. https://reference.wolfram.com/language/ref/RandomGraph.html.
APA
Wolfram 语言. (2010). RandomGraph. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/RandomGraph.html 年
BibTeX
@misc{reference.wolfram_2026_randomgraph, author="Wolfram Research", title="{RandomGraph}", year="2010", howpublished="\url{https://reference.wolfram.com/language/ref/RandomGraph.html}", note=[Accessed: 12-November-2026]}
BibLaTeX
@online{reference.wolfram_2026_randomgraph, organization={Wolfram Research}, title={RandomGraph}, year={2010}, url={https://reference.wolfram.com/language/ref/RandomGraph.html}, note=[Accessed: 12-November-2026]}