fork(1) download
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Security.Cryptography;
  5.  
  6. public class Test
  7. {
  8.  
  9. private static IEnumerable<object> Cycle(object value=null)
  10. {
  11. while (true)
  12. {
  13. yield return value;
  14. }
  15. }
  16.  
  17. private static IEnumerable<byte> RandomBytes(int chunkSize)
  18. {
  19. return Cycle().SelectMany(unused =>
  20. {
  21. var b = new byte[chunkSize];
  22. new RNGCryptoServiceProvider().GetBytes(b);
  23. return b;
  24. });
  25. }
  26.  
  27. private static IEnumerable<int> RandomWrap(int chunkSize, int limit)
  28. {
  29. int prev = 0;
  30. foreach (byte b in RandomBytes(chunkSize))
  31. {
  32. prev = (prev + b) % limit;
  33. yield return prev;
  34. }
  35.  
  36. }
  37.  
  38. private static IEnumerable<int> RandomFilter(int chunkSize, int limit)
  39. {
  40. return RandomBytes(chunkSize).Where(n => n < limit).Select(n => (int)n);
  41. }
  42.  
  43. private static IEnumerable<int> RandomModulo(int chunkSize, int limit)
  44. {
  45. return RandomBytes(chunkSize).Select(n => n % limit);
  46. }
  47.  
  48. private static IEnumerable<int> RandomResize(int chunkSize, int limit)
  49. {
  50. var denom = 256.0/limit;
  51. return RandomBytes(chunkSize).Select(n => (int)(n / denom));
  52. }
  53.  
  54. private static void TestDistribution(int limit, int amount, int chunkSize, Func<int, int, IEnumerable<int>> fn)
  55. {
  56. var counter = new Dictionary<int, int>();
  57. foreach (var i in Enumerable.Range(0, limit))
  58. {
  59. counter[i] = 0;
  60. }
  61.  
  62. foreach (var i in fn(chunkSize, limit).Take(amount))
  63. {
  64. counter[i] += 1;
  65. }
  66. Console.WriteLine($"{limit} {amount} {chunkSize} {counter.Select(kv => kv.Value).Aggregate(0, (acc, x) => acc + x)}");
  67. Console.WriteLine(string.Join(", ", from kv in counter select $"{kv.Key}"));
  68. Console.WriteLine(string.Join(", ", from kv in counter select $"{kv.Value}"));
  69. Console.WriteLine();
  70.  
  71. }
  72.  
  73. private static void Main()
  74. {
  75. Console.WriteLine("Wrap");
  76. TestDistribution(45, 100000, 100, RandomWrap);
  77. TestDistribution(32, 100000, 100, RandomWrap);
  78. Console.WriteLine();
  79.  
  80. Console.WriteLine("Filter");
  81. TestDistribution(45, 100000, 100, RandomFilter);
  82. TestDistribution(32, 100000, 100, RandomFilter);
  83. Console.WriteLine();
  84.  
  85. Console.WriteLine("Modulo");
  86. TestDistribution(45, 100000, 100, RandomModulo);
  87. TestDistribution(32, 100000, 100, RandomModulo);
  88. Console.WriteLine();
  89.  
  90. Console.WriteLine("Resize");
  91. TestDistribution(45, 100000, 100, RandomResize);
  92. TestDistribution(32, 100000, 100, RandomResize);
  93. Console.ReadLine();
  94. }
  95.  
  96. }
Success #stdin #stdout 0.3s 132480KB
stdin
Standard input is empty
stdout
Wrap
45 100000 100 100000
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44
2255, 2276, 2249, 2238, 2242, 2239, 2217, 2209, 2152, 2191, 2187, 2258, 2214, 2223, 2241, 2221, 2243, 2208, 2215, 2162, 2238, 2197, 2186, 2252, 2237, 2314, 2166, 2285, 2185, 2229, 2248, 2140, 2163, 2205, 2208, 2191, 2231, 2202, 2251, 2150, 2273, 2256, 2191, 2352, 2210

32 100000 100 100000
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
3114, 3149, 3127, 3083, 3267, 3132, 3118, 3178, 3110, 3171, 3082, 3108, 3097, 3121, 3076, 3043, 3150, 3137, 3162, 3164, 3136, 3104, 3127, 3096, 3101, 3149, 3206, 3026, 3053, 3226, 3141, 3046


Filter
45 100000 100 100000
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44
2174, 2204, 2198, 2258, 2152, 2253, 2195, 2272, 2231, 2250, 2143, 2280, 2273, 2267, 2241, 2230, 2235, 2248, 2235, 2219, 2209, 2237, 2145, 2208, 2171, 2229, 2209, 2232, 2191, 2274, 2227, 2192, 2224, 2251, 2233, 2158, 2214, 2239, 2189, 2292, 2247, 2174, 2204, 2203, 2290

32 100000 100 100000
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
3146, 3163, 3161, 3188, 3116, 3172, 3099, 3177, 3034, 3137, 3044, 3226, 3109, 3105, 3134, 3211, 3047, 3052, 3143, 3155, 3121, 3154, 3243, 3040, 3144, 3033, 3164, 3162, 3076, 3076, 3079, 3089


Modulo
45 100000 100 100000
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44
2249, 2385, 2366, 2253, 2320, 2379, 2332, 2417, 2411, 2421, 2302, 2352, 2415, 2346, 2313, 2372, 2425, 2270, 2290, 2303, 2312, 2253, 2414, 2296, 2360, 2385, 2330, 2302, 2401, 2352, 2257, 1981, 2021, 1885, 1925, 1975, 1932, 1951, 1949, 1982, 2025, 1955, 2002, 1939, 1895

32 100000 100 100000
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
3131, 3181, 3186, 3099, 3150, 3191, 3172, 3107, 3227, 3154, 3114, 3094, 3062, 2954, 3096, 3127, 3138, 3106, 3089, 3154, 3201, 3102, 3003, 3117, 3069, 3209, 3122, 3118, 3107, 3112, 3172, 3136


Resize
45 100000 100 100000
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44
2397, 2383, 2303, 1983, 2371, 2279, 1895, 2298, 2286, 1923, 2325, 2386, 1963, 2246, 2320, 2372, 1996, 2372, 2321, 1975, 2354, 2311, 1969, 2301, 2360, 1909, 2372, 2405, 1905, 2279, 2401, 2398, 1898, 2273, 2388, 1989, 2383, 2368, 1938, 2382, 2323, 2023, 2345, 2370, 1962

32 100000 100 100000
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
3172, 3060, 3143, 3118, 3135, 3135, 3088, 3151, 3179, 3200, 3076, 3090, 3217, 3190, 2988, 3111, 3168, 3184, 3122, 3084, 3050, 3069, 3178, 3136, 3058, 3095, 3129, 3153, 3110, 3132, 3111, 3168