import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.logging.LogManager; import node.Node; import analysis.NetworkDumper; public class RandomGenerator2 { public static double BIRTH_RATE = 0.50; private static int START_NODES = 100; public static List nodes = new ArrayList(); public static Random gen = new Random(); private int maxNodes = 0; private int numSpawned = 0; private int numKilled = 0; private Node firstNode; public RandomGenerator2() { firstNode = new Node(); nodes.add(firstNode); new NetworkDumper(firstNode); for (int i = 0; i < START_NODES; i++) { nodes.add(new Node()); } while (true) { if (gen.nextDouble() > 1 - BIRTH_RATE) { spawn(); } else { kill(); } try { Thread.sleep(5000); } catch (InterruptedException e) { } } // System.out.println("Executed " + ROUNDS // + " rounds (with a spawn rate of " + BIRTH_RATE + "). " // + numSpawned + " nodes were spawned, " + numKilled // + " nodes were killed. Currently " + "there are " // + nodes.size() + " nodes remaining. At one point we had" // + " a maximum of " + maxNodes + " nodes."); } private void spawn() { try { if (nodes.isEmpty()) { nodes.add(new Node()); } else { Node randomNode = getRandomNode(); Node newNode = randomNode.spawn(); nodes.add(newNode); } numSpawned++; if (maxNodes < nodes.size()) { maxNodes = nodes.size(); } } catch (IOException e) { } } private void kill() { if (nodes.size() < 2) return; Node randomNode; while ((randomNode = getRandomNode()) == firstNode) { // Dont kill first node } nodes.remove(randomNode); randomNode.leave(); numKilled++; } private Node getRandomNode() { return nodes.get(gen.nextInt(nodes.size())); } public static void main(String args[]) { System.setProperty("java.util.logging.config.file", "logging.properties"); try { LogManager.getLogManager().readConfiguration(); } catch (Exception e) { e.printStackTrace(); } new RandomGenerator2(); } }