OtherHelper.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Common
  8. {
  9. public static class OtherHelper
  10. {
  11. /// <summary>
  12. /// 秒表开始
  13. /// </summary>
  14. /// <returns></returns>
  15. public static Stopwatch SwStart()
  16. {
  17. Stopwatch sw = new Stopwatch();
  18. sw.Start();
  19. return sw;
  20. }
  21. /// <summary>
  22. /// 秒表停止返回时间
  23. /// </summary>
  24. /// <param name="sw"></param>
  25. /// <returns></returns>
  26. public static double SwStop(this Stopwatch sw)
  27. {
  28. sw.Stop();
  29. return sw.ElapsedMilliseconds;
  30. }
  31. public delegate void DelegateSw();
  32. /// <summary>
  33. /// 无参方法执行
  34. /// </summary>
  35. /// <param name="delegateSw"></param>
  36. /// <returns></returns>
  37. public static double Sw(DelegateSw delegateSw)
  38. {
  39. var sw = SwStart();
  40. delegateSw();
  41. return sw.SwStop();
  42. }
  43. /// <summary>
  44. /// 根据array生x个随机数
  45. /// </summary>
  46. /// <param name="x">生成几个随机数</param>
  47. /// <param name="array">数组 起始数字、结束数字</param>
  48. /// <returns></returns>
  49. public static IEnumerable<int> RandomInt(int x, int[] array,List<int> list)
  50. {
  51. if (array.Length != 2)
  52. yield break;
  53. for (int i = 0; i < x; i++)
  54. {
  55. var ra = new Random(Guid.NewGuid().GetHashCode());
  56. var content = ra.Next(array[0], array[1] + 1);
  57. while (list.Contains(content))
  58. {
  59. content = ra.Next(array[0], array[1] + 1);
  60. }
  61. yield return content;
  62. }
  63. }
  64. }
  65. }