BaseRandom.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //------------------------------------------------------------
  2. // All Rights Reserved , Copyright (C) 2010 , Jirisoft , Ltd.
  3. //------------------------------------------------------------
  4. using System;
  5. namespace Ant.Service.Utilities
  6. {
  7. /// <summary>
  8. /// BaseRandom
  9. /// 产生随机数
  10. ///
  11. /// 随机数管理,最大值、最小值可以自己进行设定。
  12. /// </summary>
  13. public class BaseRandom
  14. {
  15. public static int Minimum = 100000;
  16. public static int Maximal = 999999;
  17. public static int RandomLength = 6;
  18. private static string RandomString = "0123456789ABCDEFGHIJKMLNOPQRSTUVWXYZ";
  19. private static Random Random = new Random(DateTime.Now.Second);
  20. #region public static string GetRandomString() 产生随机字符
  21. /// <summary>
  22. /// 产生随机字符
  23. /// </summary>
  24. /// <returns>字符串</returns>
  25. public static string GetRandomString()
  26. {
  27. string returnValue = string.Empty;
  28. for (int i = 0; i < RandomLength; i++)
  29. {
  30. int r = Random.Next(0, RandomString.Length - 1);
  31. returnValue += RandomString[r];
  32. }
  33. return returnValue;
  34. }
  35. #endregion
  36. #region public static int GetRandom()
  37. /// <summary>
  38. /// 产生随机数
  39. /// </summary>
  40. /// <returns>随机数</returns>
  41. public static int GetRandom()
  42. {
  43. return Random.Next(Minimum, Maximal);
  44. }
  45. #endregion
  46. #region public static int GetRandom(int minimum, int maximal)
  47. /// <summary>
  48. /// 产生随机数
  49. /// </summary>
  50. /// <param name="minimum">最小值</param>
  51. /// <param name="maximal">最大值</param>
  52. /// <returns>随机数</returns>
  53. public static int GetRandom(int minimum, int maximal)
  54. {
  55. return Random.Next(minimum, maximal);
  56. }
  57. #endregion
  58. }
  59. }