温馨提示:代码在线浏览功能只能做为源码浏览参考,不能展示项目的全部,如果想更进一步了解该代码请下载:NETCMSv1.5(Build0509)完整源码版
当前文件路径:NetCMSv15/NetCMS.Common/Rand.cs

1//====================================================== 2
//== (c)2008 aspxcms inc by NeTCMS v1.0 == 3
//== Forum:bbs.aspxcms.com == 4
//== Website:www.aspxcms.com == 5
//====================================================== 6
using System; 7
using System.Collections.Generic; 8
using System.Text; 9
10
namespace NetCMS.Common 11
{ 12
public class Rand 13
{ 14
/// <summary> 15
/// 生成随机数字 16
/// </summary> 17
/// <param name="length">生成长度</param> 18
/// <returns></returns> 19
public static string Number(int Length) 20
{ 21
return Number(Length, false); 22
} 23
24
/// <summary> 25
/// 生成随机数字 26
/// </summary> 27
/// <param name="Length">生成长度</param> 28
/// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param> 29
/// <returns></returns> 30
public static string Number(int Length,bool Sleep) 31
{ 32
if(Sleep) 33
System.Threading.Thread.Sleep(3); 34
string result = ""; 35
System.Random random = new Random(); 36
for (int i = 0; i < Length; i++) 37
{ 38
result += random.Next(10).ToString(); 39
} 40
return result; 41
} 42
43
/// <summary> 44
/// 生成随机字母与数字 45
/// </summary> 46
/// <param name="IntStr">生成长度</param> 47
/// <returns></returns> 48
public static string Str(int Length) 49
{ 50
return Str(Length, false); 51
} 52
/// <summary> 53
/// 生成随机字母与数字 54
/// </summary> 55
/// <param name="Length">生成长度</param> 56
/// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param> 57
/// <returns></returns> 58
public static string Str(int Length, bool Sleep) 59
{ 60
if(Sleep) 61
System.Threading.Thread.Sleep(3); 62
char[] Pattern = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; 63
string result = ""; 64
int n = Pattern.Length; 65
System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks)); 66
for (int i = 0; i < Length; i++) 67
{ 68
int rnd = random.Next(0,n); 69
result += Pattern[rnd]; 70
} 71
return result; 72
} 73
74
75
/// <summary> 76
/// 生成随机纯字母随机数 77
/// </summary> 78
/// <param name="IntStr">生成长度</param> 79
/// <returns></returns> 80
public static string Str_char(int Length) 81
{ 82
return Str_char(Length, false); 83
} 84
85
/// <summary> 86
/// 生成随机纯字母随机数 87
/// </summary> 88
/// <param name="Length">生成长度</param> 89
/// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param> 90
/// <returns></returns> 91
public static string Str_char(int Length, bool Sleep) 92
{ 93
if (Sleep) 94
System.Threading.Thread.Sleep(3); 95
char[] Pattern = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; 96
string result = ""; 97
int n = Pattern.Length; 98
System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks)); 99
for (int i = 0; i < Length; i++) 100
{ 101
int rnd = random.Next(0, n); 102
result += Pattern[rnd]; 103
} 104
return result; 105
} 106
} 107
} 108





}