日期:2014-05-18 浏览次数:20436 次
private string GetRandomCode() { char[] chars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; string code = string.Empty; for (int i = 0; i < 4; i++) { Random rnd = new Random(GetRandomSeed()); code += chars[rnd.Next(0, 10)].ToString(); } return code; } private 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); }
------解决方案--------------------
看看注释
private string GetRandomCode() { char[] chars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; //随机数范围,填上字母就会生成字母 string code = string.Empty; for (int i = 0; i < 4; i++) //4是生成多少位的随机数,自己可以改 { Random rnd = new Random(GetRandomSeed()); code += chars[rnd.Next(0, 10)].ToString(); } return code; } private 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); }