In project development, the "random number" is generally used, but the random number in dotnet is not a true random number, and in some cases a duplicate number can be generated, and now summarizes the method of generating random numbers in the project.
1. Random Boolean values:
/// <summary> /// Random Boolean values /// </summary> /// <param name= "Random" ></param> /// <returns> Random Boolean values </returns> public static bool nextboolean (this random random) { if (random == null) { throw new argumentnullexception ("random"); } &Nbsp; return random. Nextdouble () > 0.5; }
2. Specify a random enumeration value for the enumeration type:
/// <summary> /// specifying a random enumeration value for an enumeration type /// </summary> /// <param name= "Random" ></param> /// <returns> specifying a random enumeration value for an enumeration type </returns> public static T NextEnum<T> (this random Random) where T : struct { var type = typeof (T); if (!type. Isenum) { Throw new invalidoperationexception (); } var array = system.enum.getvalues (type); var index = random. Next (Array. Getlowerbound (0), array. GetUpperBound (0) + 1); return (T) array. GetValue (index); }
3. An array of the specified lengths populated by random numbers:
/// <summary> /// random number padding for a specified length of array /// </summary> /// <param name= "Random" ></param> /// <param name= "Length" > Array length </param> /// <returns> random number padding for a specified length of array </returns> public static byte[] nextbytes (this Random random , int length) { if (length < 0) { throw&Nbsp;new argumentoutofrangeexception ("Length"); } var data = new byte[length]; random. Nextbytes (data); return data; }
4.& nbsp; random elements in an array:
/// <summary> /// random elements in an array /// </summary> /// <typeparam name= "T" > Element type </typeparam> /// <param name= "Random" ></param> /// <param name= "Items" > elements array </param> /// <returns> a random item in an array of elements </returns> public static T NextItem<T> (This random random, t[] items) { return items[random. Next (0, items. Length)]; }
< /span>
<summary> /// random time values for the specified time period /// </summary> / <param name= "Random" ></param> /// <param name= "MinValue" > The minimum value of the time range </param> <param name= "MaxValue" > The maximum value of the time range </param> /// <returns> random time values for the specified time period </returns> Public static datetime nextdatetime (This random random, datetime minvalue, datetime maxvalue) { var ticks = minValue.Ticks + (Long) (( Maxvalue.ticks - minvalue.Ticks) * random. Nextdouble ()); return new DateTime (ticks); }
6. Random Time value:
<summary>///random time value///</summary>//<param name= "random" ></param> <returns> Random time value </returns> public static DateTime nextdatetime (this random random) { Return Nextdatetime (random, Datetime.minvalue, datetime.maxvalue); }
7. Gets a string of random numbers for the specified length:
/// <summary> /// gets a string of random numbers for the specified length /// </summary> /// <param name= "Random" ></param> /// <param name= "Length" > to get random number length </param> /// <returns> Specifies the length of the random number string </returns> public static string getrandomnumberstring (this Random random, int length) { if (length < 0) { &nBsp; throw new argumentoutofrangeexception ("Length"); } char[] pattern = { ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ' }; var result = ""; var n = pattern. length; for (var i = 0; i < length; i++) { Var rnd = random. Next (0, n); result += pattern[rnd]; } return result; }
8. Gets the random alphabetic string for the specified length:
/// <summary> /// gets the specified length of the random letter string /// </summary> /// <param name= "Random" ></param> /// <param name= "Length" > to get random number length </param> /// <returns> Specifies the length of the random letter composition string </returns> public static string getrandomletterstring (this Random random, int length) { if (length < 0) { throw new argumentoutofrangeexception ("Length"); } char[] pattern = { ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ' }; var result = ""; &nbSp;var n = pattern. length; for (var i = 0; i < length; i++) { Var rnd = random. Next (0, n); result += pattern[rnd]; } return result; }
9. Gets a string of random letters and numbers for the specified length:
/// <summary> /// gets the specified length of the random letter and number string /// </summary > /// <param name= "Random" ></param> /// <param name= "Length" > to get the random number length </param > /// <returns> Specifies the length of the random letters and numbers that comprise the string </returns> public static string Getrandomletterandnumberstring (this random random, int length) { if ( length < 0) { &nBsp; throw new argumentoutofrangeexception ("Length"); } char[] pattern = { ' 0 ', ' 1 ' , ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ' }; var result = ""; var n = pattern. length; for (var i = 0; i < length; i++) { Var rnd = random. Next (0, n); result += pattern[rnd]; } return result; }
There are many ways to generate random numbers, and the corresponding random numbers can be generated according to the specific requirements.
This article is from the "Pengze 0902" blog, be sure to keep this source http://pengze0902.blog.51cto.com/7693836/1872900
Some methods of generating random numbers from dotnet