fork download
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. public class Test
  5. {
  6. public static void Main()
  7. {
  8.  
  9. // Initialise item weighting percentages
  10. Dictionary<string, int> weighting = new Dictionary<string, int>();
  11. weighting["A"] = 10; //10%
  12. weighting["B"] = 20; //20%
  13. weighting["C"] = 30; //30%
  14. weighting["D"] = 40; //40% (total = 100%)
  15.  
  16. // Initialise data set used for each iteration
  17. Dictionary<string, int> data = new Dictionary<string, int>();
  18.  
  19. // Initialise counts of the selected items
  20. Dictionary<string, int> count = new Dictionary<string, int>();
  21. count["A"] = 0;
  22. count["B"] = 0;
  23. count["C"] = 0;
  24. count["D"] = 0;
  25.  
  26. Random rand = new Random();
  27.  
  28. // Loop 5000 times
  29. for (int i = 0; i < 5000; i++) {
  30.  
  31. // For each item, get a random number between 0 and 99
  32. // and multiply it by the percentage to get a
  33. // weighted random number.
  34. data["A"] = rand.Next(100) * weighting["A"];
  35. data["B"] = rand.Next(100) * weighting["B"];
  36. data["C"] = rand.Next(100) * weighting["C"];
  37. data["D"] = rand.Next(100) * weighting["D"];
  38.  
  39. // Find which item came out on top and increment the count
  40. string sel = data.First(x => x.Value == data.Max(y => y.Value)).Key;
  41. count[sel]++;
  42.  
  43. // Log, so you can see whats going on...
  44. if (i < 15)
  45. Console.WriteLine("A:{0:00000} B:{1:00000} C:{2:00000} D:{3:00000} SELECTED:{4}",
  46. data["A"], data["B"], data["C"], data["D"], sel);
  47. else if (i == 15) Console.WriteLine("...");
  48.  
  49. }
  50.  
  51. // Output the results, showing the percentage of the number
  52. // occurrances of each item.
  53. Console.WriteLine();
  54. Console.WriteLine("Results: ");
  55. Console.WriteLine(" A = {0}%", 100 * ((double)count["A"] / (double)count.Sum(z => z.Value)));
  56. Console.WriteLine(" B = {0}%", 100 * ((double)count["B"] / (double)count.Sum(z => z.Value)));
  57. Console.WriteLine(" C = {0}%", 100 * ((double)count["C"] / (double)count.Sum(z => z.Value)));
  58. Console.WriteLine(" D = {0}%", 100 * ((double)count["D"] / (double)count.Sum(z => z.Value)));
  59. }
  60. }
Success #stdin #stdout 0.11s 15656KB
stdin
Standard input is empty
stdout
A:00780  B:00300  C:01740  D:03680  SELECTED:D
A:00600  B:00660  C:00060  D:03400  SELECTED:D
A:00900  B:01880  C:00510  D:00720  SELECTED:B
A:00260  B:01380  C:00540  D:01520  SELECTED:D
A:00220  B:01960  C:00210  D:02080  SELECTED:D
A:00020  B:01400  C:01530  D:00120  SELECTED:C
A:00980  B:00400  C:01560  D:03280  SELECTED:D
A:00330  B:00300  C:01500  D:03680  SELECTED:D
A:00590  B:00460  C:02730  D:02400  SELECTED:C
A:00580  B:01900  C:02040  D:01320  SELECTED:C
A:00620  B:01320  C:00750  D:01760  SELECTED:D
A:00320  B:01040  C:01350  D:03640  SELECTED:D
A:00340  B:01520  C:02010  D:03880  SELECTED:D
A:00850  B:01420  C:00480  D:03400  SELECTED:D
A:00560  B:00680  C:00030  D:00000  SELECTED:B
...

Results: 
    A = 1.44%
    B = 11.54%
    C = 30.6%
    D = 56.42%