C # random number class,

Source: Internet
Author: User

C # random number class,

Using System; namespace DotNet. Utilities {// <summary> /// BaseRandom // random number is generated // random number management. You can set the maximum and minimum values by yourself. /// </Summary> public class BaseRandom {public static int Minimum = 100000; public static int Maximal = 999999; public static int RandomLength = 6; private static string RandomString = "0123456789 ABCDEFGHIJKMLNOPQRSTUVWXYZ"; private static Random = new Random (DateTime. now. second); # region public static string GetRandomString () generate random characters // <summary> // generate random characters // </summary> // <returns> string </returns> public static string GetRandomString () {string returnValue = string. empty; for (int I = 0; I <RandomLength; I ++) {int r = Random. next (0, RandomString. length-1); returnValue + = RandomString [r];} return returnValue;} # endregion # region public static int GetRandom () /// <summary> // generate a Random number // </summary> /// <returns> Random number </returns> public static int GetRandom () {return Random. next (Minimum, Maximal) ;}# endregion # region public static int GetRandom (int minimum, int maximal) /// <summary> /// generate a random number /// </summary> /// <param name = "minimum"> minimum value </param> /// <param name = "maximal"> maximum value </param> // <returns> random number </returns> public static int GetRandom (int minimum, int maximal) {return Random. next (minimum, maximal) ;}# endregion }}
BaseRandom

 

Using System; namespace DotNet. utilities {// <summary> /// use the Random class to generate a pseudo-Random number // </summary> public class RandomHelper {// the random number object private Random _ Random; # region constructor // <summary> /// constructor // </summary> public RandomHelper () {// assign this to a random number object. _ random = new Random () ;}# endregion # region generates a random integer in the specified range /// <summary> /// generates a Random integer in the specified range, the random number range includes the minimum value, excluding the maximum value /// </summary> /// <param name = "minNum"> Minimum value </param> /// <param name = "maxNum"> maximum value </param> public int GetRandomInt (int minNum, int maxNum) {return this. _ random. next (minNum, maxNum );} # endregion # region generates a random decimal number between 0.0 and 1.0 /// <summary> // generates a random decimal number between 0.0 and 1.0 /// </summary> public double GetRandomDouble () {return this. _ random. nextDouble ();} # endregion # region random sorting of an array /// <summary> /// random sorting of an array /// </summary> /// <typeparam name =" T "> array type </typeparam> // <param name =" arr "> array to be sorted randomly </param> public void GetRandomArray <T> (T [] arr) {// random sorting algorithm for Arrays: randomly selects two locations and exchanges the values in the two locations, here we use the length of the array as the number of exchanges int count = arr. length; // start to switch for (int I = 0; I <count; I ++) {// generate two random number positions: int randomNum1 = GetRandomInt (0, arr. length); int randomNum2 = GetRandomInt (0, arr. length); // defines the temporary variable T temp; // exchanges the values of the two random numbers. temp = arr [randomNum1]; arr [randomNum1] = arr [randomNum2]; arr [randomNum2] = temp ;}# endregion }}
RandomHelper

 

Using System; namespace DotNet. utilities {public class RandomOperate {// a random non-repeating numeric string private int rep = 0; public string GenerateCheckCodeNum (int codeCount) {string str = string. empty; long num2 = DateTime. now. ticks + this. rep; this. rep ++; Random random = new Random (int) (ulong) num2) & 0 xffffffffL) | (int) (num2> this. rep); for (int I = 0; I <codeCount; I ++) {int num = random. next (); s Tr = str + (char) (0x30 + (ushort) (num % 10 )))). toString () ;}return str ;}// Method 2: randomly generated string (mixed numbers and letters) public string GenerateCheckCode (int codeCount) {string str = string. empty; long num2 = DateTime. now. ticks + this. rep; this. rep ++; Random random = new Random (int) (ulong) num2) & 0 xffffffffL) | (int) (num2> this. rep); for (int I = 0; I <codeCount; I ++) {char ch; int num = random. next (); if (Num % 2) = 0) {ch = (char) (0x30 + (ushort) (num % 10 )));} else {ch = (char) (0x41 + (ushort) (num % 0x1a);} str = str + ch. toString () ;}return str ;}# region /// <summary> /// random string of the specified number. /// </summary> /// <param name = "allChar"> </param> /// <param name = "CodeCount"> </param> /// <returns> </returns> private string GetRandomCode (string allChar, int CodeCount) {// string allChar = "1, 2, 3, 4, 5, 6, 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 "; string [] allCharArray = allChar. split (','); string RandomCode = ""; int temp =-1; Random rand = new Random (); for (int I = 0; I <CodeCount; I ++) {if (temp! =-1) {rand = new Random (temp * I * (int) DateTime. now. ticks);} int t = rand. next (allCharArray. length-1); while (temp = t) {t = rand. next (allCharArray. length-1);} temp = t; RandomCode + = allCharArray [t];} return RandomCode;} # endregion }}
RandomOperate

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.