package sim;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

import utils.TextFileUtils;

public class CollisionDetection
{
	public static void main(String[] args)
	{
		new CollisionDetection().execute();
	}

	private void execute()
	{
		// int numOfJobs = 1;
		int numOfMachines = 14000;
		String result = "";
		for (int i = 1; i <= numOfMachines; i++)
		{
			int collisions = calcCollisions(i, numOfMachines);
			result += i + " " + collisions + "\n";

		}
		TextFileUtils.setContents("collisions", result);
	}

	private int calcCollisions(int numOfJobs, int numOfMachines)
	{
		Set<Integer> machines = new HashSet<Integer>();
		Random random = new Random();
		int collisions = 0;
		for (int i = 0; i < numOfJobs; i++)
		{
			int machine = random.nextInt(numOfMachines);
			if (machines.contains(machine))
			{
				collisions++;
			}
			else
			{
				machines.add(machine);
			}

		}
		return collisions;
	}
}

@@ -7,16 +7,65 @@
 e sim;%0A%0A
+import java.io.File;%0Aimport java.io.IOException;%0A
 import j
@@ -232,16 +232,35 @@
 %5B%5D args)
+ throws IOException
 %0A%09%7B%0A%09%09ne
@@ -320,16 +320,35 @@
 xecute()
+ throws IOException
 %0A%09%7B%0A%09%09//
@@ -367,16 +367,179 @@
 bs = 1;%0A
+%09%09for (int i = 1; i %3C 20; i++)%0A%09%09%7B%0A%09%09%09System.out.println(%22test %22 + i);%0A%09%09%09printForRetry(i);%0A%09%09%7D%0A%09%7D%0A%0A%09private void printForRetry(int retries) throws IOException%0A%09%7B%0A
 %09%09int nu
@@ -675,24 +675,33 @@
 umOfMachines
+, retries
 );%0A%09%09%09result
@@ -733,21 +733,95 @@
 + %22%5Cn%22;%0A
-%0A
 %09%09%7D%0A
+%09%09String file = %22collisions_%22 + retries;%0A%09%09new File(file).createNewFile();%0A
 %09%09TextFi
@@ -844,20 +844,12 @@
 nts(
-%22collisions%22
+file
 , re
@@ -919,16 +919,29 @@
 Machines
+, int retries
 )%0A%09%7B%0A%09%09S
@@ -1104,34 +1104,58 @@
 e = 
-random.nextInt(numOfMachin
+findMachine(numOfMachines, random, machines, retri
 es);
@@ -1291,11 +1291,304 @@
 ions;%0A%09%7D
+%0A%0A%09private int findMachine(int numOfMachines, Random random, Set%3CInteger%3E machines, int retries)%0A%09%7B%0A%09%09int machine = 0;%0A%09%09for (int i = 0; i %3C retries; i++)%0A%09%09%7B%0A%09%09%09machine = random.nextInt(numOfMachines);%0A%09%09%09if (!machines.contains(machine))%0A%09%09%09%7B%0A%09%09%09%09return machine;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return machine;%0A%09%7D
 %0A%7D%0A

add collision detection