using System; using System.Linq; using System.Collections.Generic; public class Test { public static void Main() { // Initialise item weighting percentages Dictionary weighting = new Dictionary(); weighting["A"] = 10; //10% weighting["B"] = 20; //20% weighting["C"] = 30; //30% weighting["D"] = 40; //40% (total = 100%) // Initialise data set used for each iteration Dictionary data = new Dictionary(); // Initialise counts of the selected items Dictionary count = new Dictionary(); count["A"] = 0; count["B"] = 0; count["C"] = 0; count["D"] = 0; Random rand = new Random(); // Loop 5000 times for (int i = 0; i < 5000; i++) { // For each item, get a random number between 0 and 99 // and multiply it by the percentage to get a // weighted random number. data["A"] = rand.Next(100) * weighting["A"]; data["B"] = rand.Next(100) * weighting["B"]; data["C"] = rand.Next(100) * weighting["C"]; data["D"] = rand.Next(100) * weighting["D"]; // Find which item came out on top and increment the count string sel = data.First(x => x.Value == data.Max(y => y.Value)).Key; count[sel]++; // Log, so you can see whats going on... if (i < 15) Console.WriteLine("A:{0:00000} B:{1:00000} C:{2:00000} D:{3:00000} SELECTED:{4}", data["A"], data["B"], data["C"], data["D"], sel); else if (i == 15) Console.WriteLine("..."); } // Output the results, showing the percentage of the number // occurrances of each item. Console.WriteLine(); Console.WriteLine("Results: "); Console.WriteLine(" A = {0}%", 100 * ((double)count["A"] / (double)count.Sum(z => z.Value))); Console.WriteLine(" B = {0}%", 100 * ((double)count["B"] / (double)count.Sum(z => z.Value))); Console.WriteLine(" C = {0}%", 100 * ((double)count["C"] / (double)count.Sum(z => z.Value))); Console.WriteLine(" D = {0}%", 100 * ((double)count["D"] / (double)count.Sum(z => z.Value))); } }