首页 > 代码库 > C#随机数类
C#随机数类
using System; namespace DotNet.Utilities { 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() public static string GetRandomString() { string returnValue=http://www.mamicode.com/string.Empty; for(int i=0;i<RandomLength;i++) { int r=Random.Next(0,RandomString.Length-1); returnValue +=RandomString[r]; } return returnValue; } #region public static int GetRandom() public static int GetRandom() { return Random.Next(Minimum,Maximal); } #endregion #region public static int GetRandom(int minimum,int maximal) public static int GetRandom(int minimum,int maximal) { return Random.Next(minimum,maximal); } #endregion } }
using System; namespace DotNet.Utilities { ///使用Random类生成伪随机数 public class RandomHelper { //随机数对象 private Random _random; #region 构造函数 public RandomHelper() { //为随机数对象赋值 this._random=new Random(); } #endregion #region 生成一个指定范围的随机整数 public int GetRandomInt(int minNum,int maxNum) { return this._random.Next(minNum,int maxNum); } #endregion #region 生成一个0.0到1.0的随机小数 public double GetRandomDouble() { return this._random.NextDouble(); } #endregion #region对一个数组进行随机排序 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 } }
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)&0ffffffffL))|((int)(num2>>this.rep))); for(int i=0;i<codeCount;i++) { char ch; int num=random.Next(); if((num%2)==0 { ch=(char)(ox30+((ushort)(num%10))); } else { ch=(char)(0x41+((ushort)(num%0x1a))); } str=str+ch.ToString(); } return str; } #region private string GetRandomCode(string allChar,int CodeCount) { 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#随机数类
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。