C # Random Number class

Source: Internet
Author: User
Tags dotnet

usingSystem;namespacedotnet.utilities{ Public classbaserandom{ Public Static intminimum=100000; Public Static intMaximal=999999; Public Static intRandomlength=6;Private Static stringrandomstring="0123456789ABCDEFGHIJKMLNOPQRSTUVWXYZ";Private StaticRandom random=NewRandom (DateTime.Now.Second);#regionpublic static string Getrandomstring () Public Static stringgetrandomstring () {stringReturnvalue=string. Empty; for(intI=0; i<randomlength;i++){intR=random.next (0, randomstring.length-1); ReturnValue+=randomstring[r];}returnreturnvalue;}#regionpublic static int Getrandom () Public Static intGetrandom () {returnRandom.next (minimum,maximal);}#endregion#regionpublic static int getrandom (int minimum,int maximal) Public Static intGetrandom (intMinimumintmaximal) {returnRandom.next (minimum,maximal);}#endregion}}
usingSystem;namespacedotnet.utilities{///generating pseudo-random numbers using the random class Public classrandomhelper{//Random Number ObjectPrivateRandom _random;#regionconstructor function PublicRandomhelper () {//Assigning a value to a random number object This. _random=NewRandom ();}#endregion#regionGenerates a random integer of a specified range Public intGetrandomint (intMinnum,intmaxnum) {return  This. _random. Next (Minnum,intmaxnum);}#endregion#regionGenerate a random decimal number from 0.0 to 1.0 Public Doublegetrandomdouble () {return  This. _random. Nextdouble ();}#endregion#region对一个数组进行随机排序 Public voidGetrandomarray<t>(t[] arr) {intCount=arr. Length;//Start swapping for(intI=0; i<count;i++){//generate two random number positionsintRandomnum1=getrandomint (0, arr. Length);intRandomnum2=getrandomint (0, arr. Length);//Defining temporary variablesT temp;//Exchange values for two random number positionstemp =Arr[randomnum1]arr[randomnum1]=Arr[randomnum2];arr[randomnum2]=temp;}}#endregion}}
usingSystem;namespacedotnet.utilities{ Public classrandomoperate{//One: Randomly generated non-repeating numeric stringsPrivate intrep=0; Public stringGeneratecheckcodenum (intCodecount) {stringStr=string. Empty;Longnum2=datetime.now.ticks+ This. Rep; This. rep++; Random Random=NewRandom ((int)(((ULONG) num2) &0xffffffffL))| ((int) (num2>> This. Rep )); for(intI=0; i<codecount;i++){intnum=Random. Next (); Str=str+ ((Char)(0x30+((ushort) (num%Ten)))). ToString ();}returnstr;}//method Two: randomly generated strings (mixed numbers and letters) Public stringGeneratecheckcode (intCodecount) {stringStr=string. Empty;Longnum2=datetime.now.ticks+ This. Rep; This. rep++; Random Random=NewRandom ((int)(((ULONG) num2) &AMP;0FFFFFFFFL) | ((int) (num2>> This. Rep )); for(intI=0; i<codecount;i++){Charch;intnum=Random. Next ();if((num%2)==0{ch=(Char) (Ox30+ (ushort) (num%Ten)));}Else{ch=(Char)(0x41+((ushort) (num%0x1a)));} STR=str+Ch. ToString ();}returnstr;}#regionPrivate stringGetrandomcode (stringAllchar,intCodecount) {string[] Allchararray=allchar.split (',');stringRandomcode="";inttemp=-1; Random Rand=NewRandom (); for(intI=0; i<codecount;i++){if(Temp!=-1) {Rand-NewRandom (temp*i* (int) (DateTime.Now.Ticks)) ;}intT=rand. Next (allchararray.length-1); while(temp==t) {T=rand. Next (allchararray.length-1);} Temp=T; Randomcode+=allchararray[t];}returnRandomcode;}#endregion}}
View Code

C # Random Number class

Related Article

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.