C#随机数产生器

 1 //产生订单号的随机数
2 public class OrderNumGen
3 {
4 public string getNewOrderNum()
5 {
6 StringBuilder builder = new StringBuilder();
7 builder.Append(RandomString(4, false));
8 builder.Append("-");
9 builder.Append(RandomNumber(100, 999));
10 builder.Append("-");
11 builder.Append(RandomString(2, false));
12 return builder.ToString();
13 }
14 //size=长度,lowerCase=true 随机字母小写/false=大写
15 private string RandomString(int size, bool lowerCase)
16 {
17 StringBuilder builder = new StringBuilder();
18 Random random = new Random();
19 char ch;
20 for (int i = 0; i < size; i++)
21 {
22 ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
23 builder.Append(ch);
24 }
25 if (lowerCase)
26 return builder.ToString().ToLower();
27 return builder.ToString();
28 }
29 //产生从min到max的随机数
30 private int RandomNumber(int min, int max)
31 {
32 Random random = new Random();
33 return random.Next(min, max);
34 }
posted @ 2011-12-23 11:49  lanmiao  阅读(385)  评论(0编辑  收藏  举报