fork(1) download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Test
  6. {
  7. private static void Main()
  8. {
  9. var list = Enumerable.Range(0, 10000).ToList();
  10. var listNew = SelectRandomSample(list, 400);
  11. Console.WriteLine("Random sample: {0}", String.Join(",", listNew));
  12. }
  13.  
  14. private static List<int> SelectRandomSample(List<int> list, int sampleSize)
  15. {
  16. var result = list.Take(sampleSize).ToList();
  17. var random = new Random();
  18. for (int i = sampleSize; i < list.Count; i++)
  19. {
  20. int index = random.Next(i);
  21. if (index < sampleSize) result[index] = list[i];
  22. }
  23. return result;
  24. }
  25. }
Success #stdin #stdout 0.02s 16000KB
stdin
Standard input is empty
stdout
Random sample: 8708,7127,9815,3,6632,1560,4208,6046,5521,1515,7191,7493,4664,523,1731,3805,1170,8986,6703,7646,845,4911,3821,8250,2734,4726,5033,3753,2704,9151,6546,6980,2755,1815,1659,8672,5530,668,8397,7826,1919,1618,2498,5419,4524,3794,714,1172,5659,5967,5589,9883,9097,2054,9072,6611,3021,6893,4069,2850,8486,1105,62,7497,7120,9052,9875,787,5008,5572,1699,2913,3287,7558,7419,7969,4791,5871,9967,3719,2285,2379,82,1677,6833,1930,8566,8735,8726,7892,4711,5963,7769,3300,5624,2309,505,5630,8922,1989,8968,3331,1231,6000,495,2330,4224,7277,5477,5715,1812,679,2341,7629,7710,7359,1876,8693,4199,850,4056,8718,2621,3975,7182,7907,1873,4247,7589,4278,2557,5021,1031,2525,8248,3977,6587,8172,8292,5064,6036,8505,3850,5498,8070,8502,8685,7839,8844,4770,9136,2879,3985,4454,9941,3091,6501,4809,1128,1140,3857,1239,8071,2247,2947,4093,1190,474,8719,1509,5249,9569,2903,5765,2147,3409,176,2142,6464,9757,7293,1439,1218,7105,6829,2633,3914,1060,5079,4813,4582,569,7509,6794,4155,4939,6087,4435,3439,9293,3454,3706,5813,905,7802,3013,1591,5160,7580,9615,210,732,6261,1237,214,4276,1961,5968,904,9785,6954,6049,2008,5951,517,9826,4290,9913,8455,5392,9047,2892,6788,2078,3684,6524,792,4427,8187,9561,1175,5664,573,1861,3748,1289,9155,1153,5722,4923,8443,632,7298,9801,6699,9108,3717,2238,1557,4480,2593,8567,9655,1475,9896,4468,5017,9516,3105,4725,3989,8684,6390,3841,1684,275,3616,433,4914,1208,6421,427,2406,5921,2948,3896,7819,3524,8221,1612,4094,7089,8826,2637,5628,5100,9304,8787,2066,8553,4315,8569,5454,1264,9573,516,864,7927,9488,2253,4971,2029,7008,8664,7506,1424,2514,2736,1480,1907,5372,9149,8081,5592,3448,5274,996,9415,5880,8880,2294,5547,332,5954,1605,3573,8802,4202,3470,9904,1736,6182,5375,5559,4282,9750,8893,1097,9359,5641,3389,1146,4885,3171,4511,3818,3802,4598,5577,7688,3729,4574,6949,5738,3722,1374,366,5881,4933,369,4916,6429,6574,1343,7015,4125,8471,7457,1445,1081,6012,7676,3952,9267,4377,7944,5991,2827,8565,4175,6451,8911,8177,4032,9772,6228,1698,8100,3326,8155