[C #] RandomHelper,
Key code:
Using System; using System. text; namespace YanZhiwei. dotNet2.Utilities. common {/// <summary> /// Random help class /// </summary> public static class RandomHelper {# region generates a Random number of Double in the Set range /// <summary> /// generate a random number of Double in the Set range /// <para> eg: randomHelper. nextDouble (1.5, 1.7 ); </para> /// </summary> /// <param name = "miniDouble"> maximum number of random numbers generated </param> /// <param name = "maxiDouble"> Generate the minimum value of a random number </param> // <returns> Double </Returns> public static double NextDouble (double miniDouble, double maxiDouble) {Random _ random = new Random (int) (DateTime. now. ticks); return _ random. nextDouble () * (maxiDouble-miniDouble) + miniDouble;} # endregion # region generates a random string // <summary> // generates a random string /// <para> RandomHelper. netxtString (4, false ); </para> /// </summary> /// <param name = "size"> String Length </param> /// <param name = "lowerCase"> The string is lowerCase </param> /// <returns> random string </returns> public static string NetxtString (int size, bool lowerCase) {Random _ random = new Random (int) (DateTime. now. ticks); StringBuilder _ builder = new StringBuilder (size); int _ startChar = lowerCase? 97: 65; // 65 = A/97 = a for (int I = 0; I <size; I ++) _ builder. append (char) (26 * _ random. nextDouble () + _ startChar); return _ builder. toString () ;}# endregion # region 0 ~ 9 ~ Z string // <summary> // 0 ~ 9 ~ Z string // </summary> public static string RandomString_09AZ = "0123456789 ABCDEFGHIJKMLNOPQRSTUVWXYZ "; # endregion # region generates a random string based on the specified string /// <summary> /// generates a random string based on the specified string /// <para> RandomHelper. netxtString (RandomHelper. randomString_09AZ, 4, false ); </para> /// </summary> /// <param name = "randomString"> specify a string </param> /// <param name = "size"> string length </param> /// <param name = "lowerCase"> the string is lowerCase </param> /// <r Eturns> random string </returns> public static string NetxtString (string randomString, int size, bool lowerCase) {string _ nextString = string. empty; Random _ random = new Random (int) (DateTime. now. ticks); if (! String. isNullOrEmpty (randomString) {StringBuilder _ builder = new StringBuilder (size); int _ maxCount = randomString. length-1; for (int I = 0; I <size; I ++) {int _ number = _ random. next (0, _ maxCount); _ builder. append (randomString [_ number]);} _ nextString = _ builder. toString ();} return lowerCase? _ NextString. toLower (): _ nextString. toUpper () ;}# endregion # random time generated by region /// <summary> // random time generated /// <para> RandomHelper. nextDateTime (); </para> /// </summary> // <returns> random time </returns> public static DateTime NextDateTime () {Random _ random = new Random (int) (DateTime. now. ticks); int _ hour = _ random. next (2, 5); int _ minute = _ random. next (0, 60); int _ second = _ random. next (0, 60); string _ dateTimeString = string. format ("{0} {1 }:{ 2 }:{ 3}", DateTime. now. toString ("yyyy-MM-dd"), _ hour, _ minute, _ second); DateTime _ nextTime = Convert. toDateTime (_ dateTimeString); return _ nextTime ;}# endregion # region generates a random MAC address /// <summary> /// generates a random MAC address /// <para> RandomHelper. nextMacAddress (); </para> /// </summary> // <returns> New MAC address </returns> public static string NextMacAddress () {int _ minValue = 0, _ maxValue = 16; Random _ random = new Random (int) (DateTime. now. ticks); string _ newMacAddress = string. format ("{0} {1 }:{ 2} {3 }:{ 4} {5 }:{ 6} {7 }:{ 8} {9 }: {10} {11} ", _ random. next (_ minValue, _ maxValue ). toString ("x"), _ random. next (_ minValue, _ maxValue ). toString ("x"), _ random. next (_ minValue, _ maxValue ). toString ("x"), _ random. next (_ minValue, _ maxValue ). toString ("x"), _ random. next (_ minValue, _ maxValue ). toString ("x"), _ random. next (_ minValue, _ maxValue ). toString ("x"), _ random. next (_ minValue, _ maxValue ). toString ("x"), _ random. next (_ minValue, _ maxValue ). toString ("x"), _ random. next (_ minValue, _ maxValue ). toString ("x"), _ random. next (_ minValue, _ maxValue ). toString ("x"), _ random. next (_ minValue, _ maxValue ). toString ("x"), _ random. next (_ minValue, _ maxValue ). toString ("x ")). toUpper (); return _ newMacAddress;} # endregion }}