利用Random得到任意数字、字母和字符串,最后附上MS的Random的源码。
/// <summary>
/// Get Random number. It is used to get number over int range
/// </summary>
/// <param name="length">
/// the length of number
/// </param>
/// <returns>
/// a string included random numbers
/// </returns>
public static string GetRandomNumber(int length)
{
Random random = new Random();
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++)
{
sb.Append(random.Next(0, 10));
}
return sb.ToString();
}
//**********************************************************
/// <summary>
/// Get random char included number, lowercase, uppercase
/// 48 ~ 57: 0 ~ 9
/// 65 ~ 90: A ~ Z
/// 97 ~ 122: a ~ z
/// </summary>
/// <returns></returns>
public static char GetRandomChar()
{
Random random = new Random();
int ret = random.Next(48, 123);
while ((ret > 57 && ret < 65) || (ret > 90 && ret < 97))
{
ret = random.Next(48, 123);
}
return (char)ret;
}
//**********************************************************
/// <summary>
/// Get Random String included number, lowercase, uppercase
/// </summary>
/// <param name="length">
/// the length of string
/// </param>
/// <returns></returns>
public static string GetRandomString(int length)
{
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++)
{
sb.Append(getRandomChar());
}
return sb.ToString();
}
//**********************************************************
/*============================================================
**
** Class: Random.cs
**
**
**
** Purpose: A random number generator.
**
** Date: July 8, 1998
**
===========================================================*/
namespace System
{
using System;
using System.Runtime.CompilerServices;
[Serializable()] public class Random
{
//
// Private Constants
//
private const int MBIG = Int32.MaxValue;
private const int MSEED = 161803398;
private const int MZ = 0;
//
// Member Variables
//
private int inext, inextp;
private int[] SeedArray = new int[56];
//
// Public Constants
//
//
// Native Declarations
//
//
// Constructors
//
public Random()
: this(Environment.TickCount)
{
}
public Random(int Seed)
{
int ii;
int mj, mk;
//Initialize our Seed array.
//This algorithm comes from Numerical Recipes in C (2nd Ed.)
mj = MSEED - Math.Abs(Seed);
SeedArray[55]=mj;
mk=1;
for (int i=1; i<55; i++)
{ //Apparently the range [1..55] is special (Knuth) and so we're wasting the 0'th position.
ii = (21*i)%55;
SeedArray[ii]=mk;
mk = mj - mk;
if (mk<0) mk+=MBIG;
mj=SeedArray[ii];
}
for (int k=1; k<5; k++)
{
for (int i=1; i<56; i++)
{
SeedArray[i] -= SeedArray[1+(i+30)%55];
if (SeedArray[i]<0) SeedArray[i]+=MBIG;
}
}
inext=0;
inextp = 21;
Seed = 1;
}
//
// Package Private Methods
//
/*====================================Sample====================================
**Action: Return a new random number [0..1) and reSeed the Seed array.
**Returns: A double [0..1)
**Arguments: None
**Exceptions: None
==============================================================================*/
protected virtual double Sample()
{
int retVal;
int locINext = inext;
int locINextp = inextp;
if (++locINext >=56) locINext=1;
if (++locINextp>= 56) locINextp = 1;
retVal = SeedArray[locINext]-SeedArray[locINextp];
if (retVal<0) retVal+=MBIG;
SeedArray[locINext]=retVal;
inext = locINext;
inextp = locINextp;
//Including this division at the end gives us significantly improved
//random number distribution.
return (retVal*(1.0/MBIG));
}
//
// Public Instance Methods
//
/*====================================