123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Ropin.Inspection.Common.Helper
- {
- public class GenerateHelper
- {
- //生成随机年份
- public static int generateYear()
- {
- StringBuilder bu = new StringBuilder();
- Random rd = new Random();
- int year = rd.Next(1900, DateTime.Now.Year);
- bu.Append(year);
- return int.Parse(bu.ToString());
- }
- //生成
- public static string generate()
- {
- StringBuilder bu = new StringBuilder();
- Random rd = new Random();
- bu.Append(110000);
- int year = generateYear();
- bu.Append(year);
- int month = rd.Next(1, 12);
- if (month < 10)
- {
- bu.Append(0);
- }
- bu.Append(month);
- int[] days;
- if (isleapyear(year))
- {
- days = new int[12] { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
- }
- else
- {
- days = new int[12] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
- }
- int day = rd.Next(1, days[month]);
- if (day < 10)
- {
- bu.Append(0);
- }
- bu.Append(day);
- bu.Append(randomcode());
- int[] c = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
- char[] r = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
- string convert = bu.ToString();
- int it = 0;
- int res = 0;
- // Console.WriteLine(res);
- while (it < 17)
- {
- res = res + (convert[it] - '0') * c[it];
- // Console.WriteLine("第"+it+"次,"+convert[it]+"乘以"+c[it]);
- // Console.WriteLine(res);
- it++;
- }
- int i = res % 11;
- bu.Append(r[i]);
- return bu.ToString();
- }
- //生成3位随机
- public static string randomcode()
- {
- StringBuilder bu = new StringBuilder();
- Random rd = new Random();
- int code = rd.Next(1, 999);
- if (code < 10)
- {
- bu.Append(00);
- }
- else if (code < 100)
- {
- bu.Append(0);
- }
- bu.Append(code);
- return bu.ToString();
- }
- public static bool isleapyear(int year)
- {
- if (year % 4 == 0)
- {
- if (year % 100 == 0 && year % 400 != 0)
- {
- return false;
- }
- return true;
- }
- return false;
- }
- }
- }
|