Java實例化類詳解。本站提示廣大學習愛好者:(Java實例化類詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是Java實例化類詳解正文
本文實例講述了C#生成不反復隨機字符串類。分享給年夜家供年夜家參考。詳細以下:
這個C#類用於隨機發生不反復的字符串,可以指定字符串規模,可以指定要發生字符串的長度
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 } }
願望本文所述對年夜家的C#法式設計有所贊助。