1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 |
/// <summary> /// 产生随机字符串 /// </summary> /// <param name="num">随机出几个字符</param> /// <returns>随机出的字符串</returns> private
string GenCode( int
num) { string
str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ; //char[] chastr = str.ToCharArray(); //第二种 string
code = "" ; Random rd = new
Random(); int
i; for
(i = 0; i < num; i++) { //code += chastr[rd.Next(0, chastr.Length)]; //第二种 code += str.Substring(rd.Next(0, str.Length), 1); //第一种 } return
code; } |
最后调用即可,想出现几位就写几位
string checkCode = GenCode(6);
原文:http://www.cnblogs.com/mcho/p/3641370.html