RandomHelper.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Abp.Collections.Extensions;
  5. namespace Abp
  6. {
  7. /// <summary>
  8. /// A shortcut to use <see cref="Random"/> class.
  9. /// Also provides some useful methods.
  10. /// </summary>
  11. public static class RandomHelper
  12. {
  13. private static readonly Random Rnd = new Random();
  14. /// <summary>
  15. /// Returns a random number within a specified range.
  16. /// </summary>
  17. /// <param name="minValue">The inclusive lower bound of the random number returned.</param>
  18. /// <param name="maxValue">The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue.</param>
  19. /// <returns>
  20. /// A 32-bit signed integer greater than or equal to minValue and less than maxValue;
  21. /// that is, the range of return values includes minValue but not maxValue.
  22. /// If minValue equals maxValue, minValue is returned.
  23. /// </returns>
  24. public static int GetRandom(int minValue, int maxValue)
  25. {
  26. lock (Rnd)
  27. {
  28. return Rnd.Next(minValue, maxValue);
  29. }
  30. }
  31. /// <summary>
  32. /// Returns a nonnegative random number less than the specified maximum.
  33. /// </summary>
  34. /// <param name="maxValue">The exclusive upper bound of the random number to be generated. maxValue must be greater than or equal to zero.</param>
  35. /// <returns>
  36. /// A 32-bit signed integer greater than or equal to zero, and less than maxValue;
  37. /// that is, the range of return values ordinarily includes zero but not maxValue.
  38. /// However, if maxValue equals zero, maxValue is returned.
  39. /// </returns>
  40. public static int GetRandom(int maxValue)
  41. {
  42. lock (Rnd)
  43. {
  44. return Rnd.Next(maxValue);
  45. }
  46. }
  47. /// <summary>
  48. /// Returns a nonnegative random number.
  49. /// </summary>
  50. /// <returns>A 32-bit signed integer greater than or equal to zero and less than <see cref="int.MaxValue"/>.</returns>
  51. public static int GetRandom()
  52. {
  53. lock (Rnd)
  54. {
  55. return Rnd.Next();
  56. }
  57. }
  58. /// <summary>
  59. /// Gets random of given objects.
  60. /// </summary>
  61. /// <typeparam name="T">Type of the objects</typeparam>
  62. /// <param name="objs">List of object to select a random one</param>
  63. public static T GetRandomOf<T>(params T[] objs)
  64. {
  65. if (objs.IsNullOrEmpty())
  66. {
  67. throw new ArgumentException("objs can not be null or empty!", "objs");
  68. }
  69. return objs[GetRandom(0, objs.Length)];
  70. }
  71. /// <summary>
  72. /// Generates a randomized list from given enumerable.
  73. /// </summary>
  74. /// <typeparam name="T">Type of items in the list</typeparam>
  75. /// <param name="items">items</param>
  76. public static List<T> GenerateRandomizedList<T>(IEnumerable<T> items)
  77. {
  78. var currentList = new List<T>(items);
  79. var randomList = new List<T>();
  80. while (currentList.Any())
  81. {
  82. var randomIndex = RandomHelper.GetRandom(0, currentList.Count);
  83. randomList.Add(currentList[randomIndex]);
  84. currentList.RemoveAt(randomIndex);
  85. }
  86. return randomList;
  87. }
  88. }
  89. }