# Generate Random 3D Network using GraphPlot3D and loop

Instead of writing all this out I want to do the same thing with a loop and be able to manipulate n and k. The idea is to create a randomly generated network where n is the number of nodes and k is the number of connections between each node. I've gotten close using While and Table but haven't quite figured out how to make it work. Any suggestions?

Manipulate[
GraphPlot3D[{1 -> RandomInteger[{1, n}],
1 -> RandomInteger[{1, n}], 1 -> RandomInteger[{1, n}],
1 -> RandomInteger[{1, n}], 1 -> RandomInteger[{1, n}],
1 -> RandomInteger[{1, n}], 1 -> RandomInteger[{1, n}],
1 -> RandomInteger[{1, n}], 1 -> RandomInteger[{1, n}],
2 -> RandomInteger[{1, n}], 2 -> RandomInteger[{1, n}],
2 -> RandomInteger[{1, n}], 2 -> RandomInteger[{1, n}],
2 -> RandomInteger[{1, n}], 2 -> RandomInteger[{1, n}],
2 -> RandomInteger[{1, n}], 2 -> RandomInteger[{1, n}],
3 -> RandomInteger[{1, n}], 3 -> RandomInteger[{1, n}],
3 -> RandomInteger[{1, n}], 3 -> RandomInteger[{1, n}],
3 -> RandomInteger[{1, n}], 3 -> RandomInteger[{1, n}],
3 -> RandomInteger[{1, n}], 3 -> RandomInteger[{1, n}],
4 -> RandomInteger[{1, n}], 4 -> RandomInteger[{1, n}],
4 -> RandomInteger[{1, n}], 4 -> RandomInteger[{1, n}],
4 -> RandomInteger[{1, n}], 4 -> RandomInteger[{1, n}],
4 -> RandomInteger[{1, n}], 4 -> RandomInteger[{1, n}],
5 -> RandomInteger[{1, n}], 5 -> RandomInteger[{1, n}],
5 -> RandomInteger[{1, n}], 5 -> RandomInteger[{1, n}],
5 -> RandomInteger[{1, n}], 5 -> RandomInteger[{1, n}],
5 -> RandomInteger[{1, n}], 5 -> RandomInteger[{1, n}],
6 -> RandomInteger[{1, n}], 6 -> RandomInteger[{1, n}],
6 -> RandomInteger[{1, n}], 6 -> RandomInteger[{1, n}],
6 -> RandomInteger[{1, n}], 6 -> RandomInteger[{1, n}],
6 -> RandomInteger[{1, n}], 6 -> RandomInteger[{1, n}],
7 -> RandomInteger[{1, n}], 7 -> RandomInteger[{1, n}],
7 -> RandomInteger[{1, n}], 7 -> RandomInteger[{1, n}],
7 -> RandomInteger[{1, n}], 7 -> RandomInteger[{1, n}],
7 -> RandomInteger[{1, n}], 7 -> RandomInteger[{1, n}],
7 -> RandomInteger[{1, n}],
8 -> RandomInteger[{1, n}], 8 -> RandomInteger[{1, n}],
8 -> RandomInteger[{1, n}], 8 -> RandomInteger[{1, n}],
8 -> RandomInteger[{1, n}], 8 -> RandomInteger[{1, n}],
8 -> RandomInteger[{1, n}], 8 -> RandomInteger[{1, n}]
},
VertexRenderingFunction -> ({White, EdgeForm[Black], Sphere[#, .1],
Black, Text[#2, #1]} &), Boxed -> False], {n, 1, 8, 1}, {k, 1, 8, 1}]

• GraphPlot3D@RandomGraph[{n,k}]. "Random network" is not meaningful by itself. There are many random graph models, and they lead to different distributions. What you wrote does not generate all labelled graphs with n vertices and k edges with equal probability. Commented Jul 2, 2014 at 21:22
• Are the networks you are working with Boolean NK networks? If yes, there is a demonstration. Commented Jul 3, 2014 at 4:32
• @KennyColnago. Yes, this demonstration is a good start, but I'm a looking to do something that expands on it. For example, you can only select N: 50, 100, or 200. What if I want to choose N: 53 or 137? Commented Jul 8, 2014 at 14:28
• @Szabolcs. That is true I suppose, does it really matter? How would you suggest creating a model with equal probability? As you can see, I am very new to Mathematica and simulating networks. I'm open to constructive criticism and discussing any ideas on different directions to go in. Commented Jul 8, 2014 at 14:37
• "does it really matter?" <-- Well, that depends on what you want to do with the network and it's up to you to decide. If you want to use it for research, then yes, it matters a lot, as the type of random graph model you use will influence the results you get. In Mathematica, RandomGraph[{n,k}] ensures uniform distribution. Commented Jul 8, 2014 at 14:42

If you are just asking how to reproduce your code more compactly/parametrically, you can use RandomInteger to produce a list of integers then rearrange.

For now let's just use n = 5. We can generate the rules by creating two long lists and then threading them together. Here's the list of left-hand sides (of the rules):

nodes = Sort@Flatten@Table[Range@8, {8}]


{1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8}

And right-hand sides:

rand = RandomInteger[{1, n}, 64]


{3,5,4,4,5,1,4,5,3,2,4,1,1,3,3,3,1,4,5,5,1,4,1,4,3,2,1,4,4,5,3,3,5,4,5,5,4,5,2,1,5,5,4,4,4,2,3,2,5,5,1,1,5,4,1,2,4,4,3,2,1,2,3,2}

We will apply a function as we thread them:

rules = MapThread[Rule, {nodes, rand}]


{1->3,1->5,1->4,1->4,1->5,1->1,1->4,1->5,2->3,2->2,2->4,2->1,2->1,2->3,2->3,2->3,3->1,3->4,3->5,3->5,3->1,3->4,3->1,3->4,4->3,4->2,4->1,4->4,4->4,4->5,4->3,4->3,5->5,5->4,5->5,5->5,5->4,5->5,5->2,5->1,6->5,6->5,6->4,6->4,6->4,6->2,6->3,6->2,7->5,7->5,7->1,7->1,7->5,7->4,7->1,7->2,8->4,8->4,8->3,8->2,8->1,8->2,8->3,8->2}

Now we can throw that into the plotting function:

GraphPlot3D[
rules,
VertexRenderingFunction -> ({White, EdgeForm[Black], Sphere[#, .1],
Black, Text[#2, #1]} &), Boxed -> False]


• Thanks, man. There is some useful language in there I was not familiar with. Commented Jul 8, 2014 at 15:09