using System; namespace DotNet.Utilities { /// <summary> /// BaseRandom /// 產生隨機數 /// /// 隨機數管理,最大值、最小值可以自己進行設定。 /// </summary> public class BaseRando ...
using System; namespace DotNet.Utilities { /// <summary> /// BaseRandom /// 產生隨機數 /// /// 隨機數管理,最大值、最小值可以自己進行設定。 /// </summary> public class BaseRandom { public static int Minimum = 100000; public static int Maximal = 999999; public static int RandomLength = 6; private static string RandomString = "0123456789ABCDEFGHIJKMLNOPQRSTUVWXYZ"; private static Random Random = new Random(DateTime.Now.Second); #region public static string GetRandomString() 產生隨機字元 /// <summary> /// 產生隨機字元 /// </summary> /// <returns>字元串</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> /// 產生隨機數 /// </summary> /// <returns>隨機數</returns> public static int GetRandom() { return Random.Next(Minimum, Maximal); } #endregion #region public static int GetRandom(int minimum, int maximal) /// <summary> /// 產生隨機數 /// </summary> /// <param name="minimum">最小值</param> /// <param name="maximal">最大值</param> /// <returns>隨機數</returns> public static int GetRandom(int minimum, int maximal) { return Random.Next(minimum, maximal); } #endregion } }BaseRandom
using System; namespace DotNet.Utilities { /// <summary> /// 使用Random類生成偽隨機數 /// </summary> public class RandomHelper { //隨機數對象 private Random _random; #region 構造函數 /// <summary> /// 構造函數 /// </summary> public RandomHelper() { //為隨機數對象賦值 this._random = new Random(); } #endregion #region 生成一個指定範圍的隨機整數 /// <summary> /// 生成一個指定範圍的隨機整數,該隨機數範圍包括最小值,但不包括最大值 /// </summary> /// <param name="minNum">最小值</param> /// <param name="maxNum">最大值</param> public int GetRandomInt(int minNum, int maxNum) { return this._random.Next(minNum, maxNum); } #endregion #region 生成一個0.0到1.0的隨機小數 /// <summary> /// 生成一個0.0到1.0的隨機小數 /// </summary> public double GetRandomDouble() { return this._random.NextDouble(); } #endregion #region 對一個數組進行隨機排序 /// <summary> /// 對一個數組進行隨機排序 /// </summary> /// <typeparam name="T">數組的類型</typeparam> /// <param name="arr">需要隨機排序的數組</param> public void GetRandomArray<T>(T[] arr) { //對數組進行隨機排序的演算法:隨機選擇兩個位置,將兩個位置上的值交換 //交換的次數,這裡使用數組的長度作為交換次數 int count = arr.Length; //開始交換 for (int i = 0; i < count; i++) { //生成兩個隨機數位置 int randomNum1 = GetRandomInt(0, arr.Length); int randomNum2 = GetRandomInt(0, arr.Length); //定義臨時變數 T temp; //交換兩個隨機數位置的值 temp = arr[randomNum1]; arr[randomNum1] = arr[randomNum2]; arr[randomNum2] = temp; } } #endregion } }RandomHelper
using System; namespace DotNet.Utilities { public class RandomOperate { // 一:隨機生成不重覆數字字元串 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) & 0xffffffffL)) | ((int)(num2 >> this.rep))); for (int i = 0; i < codeCount; i++) { int num = random.Next(); str = str + ((char)(0x30 + ((ushort)(num % 10)))).ToString(); } return str; } //方法二:隨機生成字元串(數字和字母混和) 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) & 0xffffffffL)) | ((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> /// 從字元串里隨機得到,規定個數的字元串. /// </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,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"; 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