[C #] Helper encapsulation,
1 using System; 2 3 namespace Wen. helpers 4 {5 /// <summary> 6 // random number assistant 7 /// </summary> 8 public sealed class RandomHelper 9 {10 // <summary> 11/ // Character Set combination (remove the confusing characters 0oO) 12 /// </summary> 13 private const string CharSet = "123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ ~! @ # $ % ^ & * () _ + | "; 14 15 private static readonly Random = new Random (); 16 17 # region Next18 19 /// <summary> 20 // return a nonnegative Random Number 21 /// </summary> 22 /// <returns> greater than or equal to zero and less than System. int32.MaxValue 32-bit signed integer </returns> 23 public static int Next () 24 {25 return Random. next (); 26} 27 28 /// <summary> 29 // returns a random number 30 in the specified range /// </summary> 31 /// <param name = "maxValue"> The upper limit of the random number to be generated (the upper limit cannot be set for the random number ). MaxValue must be greater than or equal to zero </param> 32 // <returns> 33 // a 32-bit signed integer greater than or equal to zero and less than maxValue, that is: the returned value range is usually zero, but not maxValue. However, if maxValue is equal to zero, maxValue is returned. 34 /// </returns> 35 public static int Next (int maxValue) 36 {37 return Random. next (maxValue ); 38} 39 40 // <summary> 41 // return a random number 42 in the specified range // </summary> 43 // <param name = "minValue"> lower bound of the returned random number (this lower bound value is recommended for a random number) </param> 44 // <param name = "maxValue"> upper bound of the returned random number (the upper bound of the random number cannot be used ). MaxValue must be greater than or equal to minValue </param> 45 // <returns> 46 // a 32-bit signed integer greater than or equal to minValue and less than maxValue, that is: the returned value ranges from minValue to maxValue. 47 // If minValue is equal to maxValue, minValue is returned. 48 /// </returns> 49 public static int Next (int minValue, int maxValue) 50 {51 return Random. next (minValue, maxValue ); 52} 53 54 # endregion Next55 56 // <summary> 57 // return a random number between 0.0 and 1.0. 58 /// </summary> 59 // <returns> double-precision floating point number greater than or equal to 0.0 and less than 1.0 </returns> 60 public static double NextDouble () 61 {62 return Random. nextDouble (); 63} 64 65 // <summary> 66 // return a random string 67 // </summary> 68 // <param name = "length"> String length </param> 69 // <returns> </returns> 70 public static string NextString (int length) 71 {72 var arr = new char [length]; 73 74 for (var I = 0; I <length; I ++) 75 {76 var index = Random. next (CharSet. length); 77 arr [I] = CharSet [index]; 78} 79 80 return string. join ("", arr); 81} 82} 83}