fork(1) download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace ConsoleApplication2
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. var arr = Enumerable.Range(1, 1000).ToArray();
  12.  
  13. for (int i = 0; i < 100; i++)
  14. {
  15. var element = arr.GetRandomSample(1).Single();
  16. Console.WriteLine(element);
  17. }
  18. }
  19. }
  20.  
  21. public static class X
  22. {
  23. private static readonly Random Random = new Random(1);
  24. public static IEnumerable<T> GetRandomSample<T>(this IReadOnlyCollection<T> source, int maxCount)
  25. {
  26. var numbersPicked = 0;
  27. int i = 0;
  28. foreach (var number in source)
  29. {
  30. var prob = (maxCount - numbersPicked) / (double) (source.Count - i);
  31. if (Random.NextDouble() < prob)
  32. {
  33. yield return number;
  34. numbersPicked++;
  35. }
  36. }
  37. }
  38. }
  39. }
  40.  
Runtime error #stdin #stdout #stderr 0.02s 136448KB
stdin
Standard input is empty
stdout
655
117
194
stderr
Unhandled Exception:
System.InvalidOperationException: Sequence contains no elements
  at System.Linq.Enumerable.Single[TSource] (System.Collections.Generic.IEnumerable`1[T] source) [0x00061] in <63992662b765477a898ef49cdcc99ee2>:0 
  at ConsoleApplication2.Program.Main (System.String[] args) [0x0001f] in <bed13f86c5874498b567c259345cd57b>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidOperationException: Sequence contains no elements
  at System.Linq.Enumerable.Single[TSource] (System.Collections.Generic.IEnumerable`1[T] source) [0x00061] in <63992662b765477a898ef49cdcc99ee2>:0 
  at ConsoleApplication2.Program.Main (System.String[] args) [0x0001f] in <bed13f86c5874498b567c259345cd57b>:0