. NET generates random numbers

Source: Internet
Author: User

Using System;
Using System.Configuration;
Using System.Text;
Using System.Data;

Namespace ZC. Utils
{
<summary>
A summary description of the Assistant.
</summary>
public sealed class Randomhelper
{

#region

<summary>
Randomly obtained from a string, the specified number of strings.
</summary>
<param name= "Allchar" ></param>
<param name= "Codecount" ></param>
<returns></returns>
public static 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,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;
}

<summary>
Randomly obtained from a string, the specified number of strings.
</summary>
<param name= "Allchar" ></param>
<param name= "Codecount" ></param>
<returns></returns>
public static string Getrandomcode (int codecount)
{
String Allchar = "1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,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 (Getrandomseed ());
for (int i = 0; i < Codecount; i++)
{
if (temp! =-1)
{
Rand = new Random (temp * i * ((int) DateTime.Now.Ticks));
Rand = new Random (Getrandomseed ());
}

int t = rand. Next (allchararray.length-1);

while (temp = = t)
{
t = rand. Next (allchararray.length-1);
}

temp = t;
Randomcode + = allchararray[t];
}
return randomcode;
}


static int Getrandomseed ()
{
byte[] bytes = new Byte[4];
System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider ();
Rng. GetBytes (bytes);
Return Bitconverter.toint32 (bytes, 0);
}

#endregion


}
}

. NET generates random numbers

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.