Skip to content

Instantly share code, notes, and snippets.

@ngopal
Created September 9, 2014 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngopal/94099d0168bb1bd387f1 to your computer and use it in GitHub Desktop.
Save ngopal/94099d0168bb1bd387f1 to your computer and use it in GitHub Desktop.
A JS implementation of creating a Erdos-Renyi (random) network. Planning to add others.
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function erdosRenyi(nodesNum,directedness,prob) {
// create nodes list
var nodes = []
for (var i = 0; i < nodesNum; i++) {
nodes.push(i);
}
// init edges
var edges = [];
for (var i = 0; i < nodes.length; i++) {
for (var j = i+1; j < nodes.length; j++) {
var pchance = Math.random()
if (pchance > prob) {
if (directedness === 0) {
edges.push({"source":i,"target":j,"directedness":0});
}
else {
var dchance = Math.random();
if (dchance > prob) {
edges.push({"source":i,"target":j,"directedness":1})
edges.push({"source":j,"target":i,"directedness":1})
}
else {
edges.push({"source":i,"target":j,"directedness":1})
// need to fix - can't be i or j!
var rando = getRandomInt(0,nodes.length);
edges.push({"source":getRandomInt(0,nodes.length),"target":i,"directedness":1})
}
}
}
}
}
return {"nodes":nodes.map(function(m) { return {"name":m.toString()};}), "edges":edges};
}
erdosRenyi(40,1,0.4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment