using System;
using System.Text;
using System.Collections.Generic;
using System.Security.Cryptography;
namespace Utils
{
public class Rnd
{
[Flags]
public enum CharType
{
None = 0,
Numbers = 1,
LowerLetter = 2,
UpperLetter = 4,
Symbol = 8,
Chinese = 16
}
public Rnd(int count, CharType type)
{
_count = count;
_type = type;
}
protected int _count;
protected CharType _type;
public int Count
{
get { return _count; }
}
public CharType Type
{
get { return _type; }
}
public string NextChars()
{
return Rnd.GetChars(Count, Type);
}
protected char NextChar()
{
return Rnd.GetChar(Type);
}
#region Static Members
private static byte[] randb = new byte[4];
private static RNGCryptoServiceProvider rand = new RNGCryptoServiceProvider();
private static char[] SYMBOLS = new char[] {
'~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_',
'+', '|', '\\', '=', '-', ',', '.', '/', ';', '\'', '[', ']',
'<', '>', '?', ':', '"', '{', '}'
};
private static char[] BASE = new char[16] {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
private static Encoding gb = Encoding.GetEncoding("gb2312");
public static char GetChar(CharType type)
{
return GetChar2(GetCharTypes(type));
}
public static string GetChars(int count, CharType type)
{
CharType[] a = GetCharTypes(type);
if (a.Length > 0)
{
StringBuilder chars = new StringBuilder();
char c;
for (int i = 0; i < count; )
{
c = GetChar2(a);
if (c != '\0')
{
chars.Append(c);
i++;
}
}
return chars.ToString();
}
else
{
return string.Empty;
}
}
static CharType[] GetCharTypes(CharType type)
{
List<CharType> a = new List<CharType>();
int b = (int)type;
foreach (int c in Enum.GetValues(typeof(CharType)))
{
int e = c & b;
if (e != 0)
{
a.Add((CharType)e);
}
}
CharType[] d = new CharType[a.Count];
a.CopyTo(d);
return d;
}
static char GetChar2(CharType[] type)
{
CharType a = type[Next(0, type.Length)];
switch (a)
{
case CharType.Numbers:
return GetNumber();
case CharType.LowerLetter:
return GetLowerLetter();
case CharType.UpperLetter:
return GetUpperLetter();
case CharType.Symbol:
return GetSymbol();
case CharType.Chinese:
return GetChinese();
default:
return '\0';
}
}
public static char GetNumber()
{
return (char)Next((int)'0', (int)'9');
}
public static char GetLowerLetter()
{
return (char)Next((int)'a', (int)'z');
}
public static char GetUpperLetter()
{
return (char)Next((int)'A', (int)'Z');
}
public static char GetSymbol()
{
return SYMBOLS[Next(0, SYMBOLS.Length)];
}
public static char GetChinese()
{
string b1, b2;
int i;
i = Next(11, 14);
b1 = BASE[i].ToString();
if (i == 13)
b1 += BASE[Next(0, 7)];
else
b1 += BASE[Next(0, 16)];
i = Next(10, 16);
b2 = BASE[i].ToString();
if (i == 10)
b2 += BASE[Next(1, 16)].ToString();
else if (i == 15)
b2 += BASE[Next(0, 15)].ToString();
else
b2 += BASE[Next(0, 16)].ToString();
byte[] b = new byte[2];
b[0] = Convert.ToByte(b1, 16);
b[1] = Convert.ToByte(b2, 16);
char[] chars = gb.GetString(b).ToCharArray();
return chars[0];
}
public static int Next()
{
rand.GetBytes(randb);
int value = BitConverter.ToInt32(randb, 0);
if (value < 0) value = -value;
return value;
}
public static int Next(int max)
{
rand.GetBytes(randb);
int value = BitConverter.ToInt32(randb, 0);
value = value % max;
if (value < 0) value = -value;
return value;
}
public static int Next(int min, int max)
{
int value = Next(max - min) + min;
return value;
}
#endregion
}
}