fork 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<long> RandomLongs()
  28. {
  29. var csrng = new RNGCryptoServiceProvider();
  30. return Cycle().Select(unused =>
  31. {
  32. var bytes = new byte[4];
  33. csrng.GetBytes(bytes);
  34. return (long)(BitConverter.ToUInt32( bytes, 0));
  35. });
  36. }
  37.  
  38. private static IEnumerable<int> RandomExtended(int chunkSize, int limit)
  39. {
  40. var source = RandomBytes(chunkSize);
  41. foreach (long lng in RandomLongs())
  42. {
  43. var nxt = lng % limit;
  44. yield return (int)nxt;
  45. }
  46.  
  47. }
  48.  
  49. private static IEnumerable<int> RandomWrap(int chunkSize, int limit)
  50. {
  51. int prev = 0;
  52. foreach (byte b in RandomBytes(chunkSize))
  53. {
  54. prev = (prev + b) % limit;
  55. yield return prev;
  56. }
  57.  
  58. }
  59.  
  60. private static IEnumerable<int> RandomFilter(int chunkSize, int limit)
  61. {
  62. return RandomBytes(chunkSize).Where(n => n < limit).Select(n => (int)n);
  63. }
  64.  
  65. private static IEnumerable<int> RandomModulo(int chunkSize, int limit)
  66. {
  67. return RandomBytes(chunkSize).Select(n => n % limit);
  68. }
  69.  
  70. private static IEnumerable<int> RandomResize(int chunkSize, int limit)
  71. {
  72. var denom = 256.0/limit;
  73. return RandomBytes(chunkSize).Select(n => (int)(n / denom));
  74. }
  75.  
  76. private static void TestDistribution(int limit, int amount, int chunkSize, Func<int, int, IEnumerable<int>> fn)
  77. {
  78. var counter = new Dictionary<int, int>();
  79. foreach (var i in Enumerable.Range(0, limit))
  80. {
  81. counter[i] = 0;
  82. }
  83.  
  84. foreach (var i in fn(chunkSize, limit).Take(amount))
  85. {
  86. counter[i] += 1;
  87. }
  88. Console.WriteLine($"{limit} {amount} {chunkSize} {counter.Select(kv => kv.Value).Aggregate(0, (acc, x) => acc + x)}");
  89. Console.WriteLine(string.Join(", ", from kv in counter select $"{kv.Key}"));
  90. Console.WriteLine(string.Join(", ", from kv in counter select $"{kv.Value}"));
  91. Console.WriteLine();
  92.  
  93. }
  94.  
  95. private static void Main()
  96. {
  97. Console.WriteLine("Extended");
  98. TestDistribution(45, 100000, 100, RandomExtended);
  99. TestDistribution(32, 100000, 100, RandomExtended);
  100. Console.WriteLine();
  101.  
  102. Console.WriteLine("Wrap");
  103. TestDistribution(45, 100000, 100, RandomWrap);
  104. TestDistribution(32, 100000, 100, RandomWrap);
  105. Console.WriteLine();
  106.  
  107. Console.WriteLine("Filter");
  108. TestDistribution(45, 100000, 100, RandomFilter);
  109. TestDistribution(32, 100000, 100, RandomFilter);
  110. Console.WriteLine();
  111.  
  112. Console.WriteLine("Modulo");
  113. TestDistribution(45, 100000, 100, RandomModulo);
  114. TestDistribution(32, 100000, 100, RandomModulo);
  115. Console.WriteLine();
  116.  
  117. Console.WriteLine("Resize");
  118. TestDistribution(45, 100000, 100, RandomResize);
  119. TestDistribution(32, 100000, 100, RandomResize);
  120. Console.ReadLine();
  121. }
  122.  
  123. }
Success #stdin #stdout 0.56s 132928KB
stdin
Standard input is empty
stdout
Extended
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
2203, 2232, 2221, 2173, 2153, 2255, 2226, 2216, 2227, 2161, 2160, 2277, 2213, 2259, 2199, 2335, 2197, 2299, 2063, 2216, 2205, 2269, 2253, 2236, 2184, 2224, 2234, 2162, 2157, 2190, 2168, 2205, 2259, 2294, 2168, 2169, 2236, 2225, 2225, 2296, 2185, 2264, 2245, 2333, 2329

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
3161, 3112, 3186, 3234, 3078, 3130, 3018, 3120, 3051, 3065, 3085, 3076, 3166, 3230, 3137, 3179, 3071, 3029, 3103, 3127, 3121, 3160, 3115, 3094, 3178, 3105, 3226, 3233, 3138, 3071, 3125, 3076


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
2280, 2244, 2201, 2171, 2211, 2185, 2172, 2364, 2153, 2282, 2308, 2215, 2228, 2240, 2212, 2291, 2298, 2233, 2172, 2260, 2265, 2226, 2239, 2179, 2273, 2156, 2120, 2234, 2219, 2228, 2176, 2228, 2179, 2203, 2198, 2197, 2180, 2270, 2302, 2187, 2293, 2254, 2080, 2177, 2217

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
3166, 3100, 3162, 3080, 3155, 3105, 3190, 3114, 3087, 3121, 3156, 3118, 3130, 3113, 3194, 3083, 3179, 3197, 3184, 3091, 3113, 3194, 3084, 3130, 3105, 3073, 3082, 3068, 3129, 3079, 3081, 3137


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
2252, 2182, 2226, 2249, 2208, 2277, 2164, 2202, 2268, 2242, 2280, 2222, 2253, 2291, 2207, 2233, 2234, 2179, 2271, 2112, 2203, 2223, 2180, 2249, 2188, 2300, 2264, 2186, 2232, 2220, 2211, 2240, 2157, 2179, 2235, 2224, 2194, 2222, 2186, 2198, 2277, 2167, 2303, 2175, 2235

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
2988, 3082, 3016, 3123, 3234, 3204, 3202, 3246, 3100, 3025, 3207, 3070, 3117, 3123, 3157, 3170, 3142, 3129, 3132, 3040, 3144, 3113, 3154, 3151, 2999, 3118, 3075, 3198, 3140, 3087, 3148, 3166


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
2331, 2371, 2304, 2295, 2298, 2320, 2339, 2326, 2363, 2308, 2366, 2356, 2328, 2278, 2307, 2352, 2387, 2356, 2484, 2373, 2342, 2293, 2373, 2327, 2509, 2342, 2283, 2316, 2312, 2317, 2298, 1922, 1907, 1884, 2031, 1873, 1936, 1982, 2014, 2042, 1991, 1960, 1933, 2016, 1955

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
3151, 3128, 3198, 3126, 3102, 3052, 3150, 3171, 3087, 3067, 3267, 3115, 3097, 3021, 3134, 3058, 3078, 3117, 3129, 3137, 3112, 3131, 3122, 3139, 3114, 3211, 3127, 3132, 3108, 3205, 3112, 3102


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
2400, 2301, 2348, 1960, 2272, 2308, 1938, 2372, 2392, 1945, 2385, 2389, 1961, 2312, 2231, 2420, 1992, 2291, 2275, 1958, 2379, 2321, 1953, 2389, 2311, 1914, 2436, 2348, 1960, 2284, 2269, 2333, 1912, 2403, 2356, 1874, 2321, 2432, 2019, 2310, 2383, 1949, 2330, 2350, 2014

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
3115, 3153, 3167, 3126, 3116, 3012, 3131, 3175, 3140, 3175, 3124, 3123, 3174, 3115, 3087, 3161, 2991, 3108, 3142, 3075, 3092, 3049, 3145, 3224, 3077, 3150, 3149, 3133, 3158, 3119, 3197, 3097