Advertisement
Guest User

Untitled

a guest
Jul 7th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. using System;
  2. using System.Security.Cryptography;
  3. ///<summary>
  4. /// Represents a pseudo-random number generator, a device that produces random data.
  5. ///</summary>
  6. classCryptoRandom : RandomNumberGenerator
  7. {
  8. privatestaticRandomNumberGenerator r;
  9. ///<summary>
  10. /// Creates an instance of the default implementation of a cryptographic random number generator that can be used to generate random data.
  11. ///</summary>
  12. public CryptoRandom()
  13. {
  14. r = RandomNumberGenerator.Create();
  15. }
  16. ///<summary>
  17. /// Fills the elements of a specified array of bytes with random numbers.
  18. ///</summary>
  19. ///<param name=”buffer”>An array of bytes to contain random numbers.</param>
  20. publicoverridevoid GetBytes(byte[] buffer)
  21. {
  22. r.GetBytes(buffer);
  23. }
  24. ///<summary>
  25. /// Returns a random number between 0.0 and 1.0.
  26. ///</summary>
  27. publicdouble NextDouble()
  28. {
  29. byte[] b = newbyte[4];
  30. r.GetBytes(b);
  31. return (double)BitConverter.ToUInt32(b, 0) / UInt32.MaxValue;
  32. }
  33. ///<summary>
  34. /// Returns a random number within the specified range.
  35. ///</summary>
  36. ///<param name=”minValue”>The inclusive lower bound of the random number returned.</param>
  37. ///<param name=”maxValue”>The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue.</param>
  38. publicint Next(int minValue, int maxValue)
  39. {
  40. return (int)Math.Round(NextDouble() * (maxValue – minValue – 1)) + minValue;
  41. }
  42. ///<summary>
  43. /// Returns a nonnegative random number.
  44. ///</summary>
  45. publicint Next()
  46. {
  47. return Next(0, Int32.MaxValue);
  48. }
  49. ///<summary>
  50. /// Returns a nonnegative random number less than the specified maximum
  51. ///</summary>
  52. ///<param name=”maxValue”>The inclusive upper bound of the random number returned. maxValue must be greater than or equal 0</param>
  53. publicint Next(int maxValue)
  54. {
  55. return Next(0, maxValue);
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement