简单生成随机测试数据

    public class Test
    {
        private static Random _rd = new Random();
        public Guid Id { get; set; }
        public string Name { get; set; }
        public bool Check { get; set; }
        public string Title { get; set; }
        public DateTime CheckTime { get; set; }

        public static List<Test> Demo() {
            List<Test> test = new List<Test>();
            for (int i = 0; i < _rd.Next(10,50); i++)
            {
                test.Add(new Test() { Id = Guid.NewGuid(), Name = RandomText(), Title = RandomText(5, 7), Check = RandomBool(), CheckTime = DateTime.Now.AddDays(_rd.Next(-5000,5000)) });
            }
            return test;
        }

        private static string RandomText(int min=5,int max=20)
        {
            string s = "";
            for (int i = 0; i < _rd.Next(min, max); i++)
            {
                s += (char)RandomInt();
            }
            return s;
        }

        private static bool RandomBool()
        {
            return Convert.ToBoolean(_rd.Next(0, 1));
        }
        private static int RandomInt()
        {
            int i = _rd.Next(48, 122);
            if (i > 47 && i < 58) // Number
            {
                return i;
            }
            else if (i > 64 && i < 91) // Upper case letters
            {
                return i;
            }
            else if (i > 96 && i < 123) // Lower case letters
            {
                return i;
            }
            else
            {
                return RandomInt(); // Retry Random
            }
        }

    }

字符串来源:

for (int i = 48; i < 123; i++)
            {
                Console.Write($"{i}={Convert.ToChar(i)} ");
            }
            

 

48=0 49=1 50=2 51=3 52=4 53=5 54=6 55=7 56=8 57=9 58=: 59=; 60=< 61== 62=> 63=? 64=@ 65=A 66=B 67=C 68=D 69=E 70=F 71=G 72=H 73=I 74=J 75=K 76=L 77=M 78=N 79=O 80=P 81=Q 82=R 83=S 84=T 85=U 86=V 87=W 88=X 89=Y 90=Z 91=[ 92=\ 93=] 94=^ 95=_ 96=` 97=a 98=b 99=c 100=d 101=e 102=f 103=g 104=h 105=i 106=j 107=k 108=l 109=m 110=n 111=o 112=p 113=q 114=r 115=s 116=t 117=u 118=v 119=w 120=x 121=y 122=z

 

使用方法:

        private void RefreshData()
        {
            var ds = Test.Demo();
            label1.DataBindings.Clear();
            checkBox1.DataBindings.Clear();
            dateTimePicker1.DataBindings.Clear();
            button1.DataBindings.Clear();
            
            label1.DataBindings.Add("Text", ds, "Name");
            checkBox1.DataBindings.Add("Checked", ds, "Check");
            dateTimePicker1.DataBindings.Add("Value", ds, "CheckTime");
            button1.DataBindings.Add("Text", ds, "Title");
            button1.DataBindings.Add("Tag", ds, "Id");

            dataRepeater1.DataSource = ds;

        }

 

 

随机类:

    internal static class Random
    {
        private static System.Random _rd = new System.Random();
        internal static string Text(int min = 5, int max = 20)
        {
            string s = "";
            for (int i = 0; i < _rd.Next(min, max); i++)
            {
                s += (char)Char();
            }
            return s;
        }

        internal static string[] Text(int Count)
        {
            var arr = new System.Collections.Generic.List<string>();
            for (int i = 0; i < Count; i++)
            {
                arr.Add(Text(5, 20));
            }
            return arr.ToArray();
        }

        internal static System.Data.DataTable Table()
        {
            var cc = _rd.Next(10, 20);
            var t = new System.Data.DataTable();
            for (int i = 0; i < cc; i++)
            {
                t.Columns.Add(Text(5, 20));
            }
            var rc = _rd.Next(50, 100);
            for (int i = 0; i < rc; i++)
            {
                var r = t.LoadDataRow(Text(cc), false);
            }
            return t;
        }

        internal static int Char()
        {
            int i = _rd.Next(48, 122);
            if (i > 47 && i < 58) // Number
            {
                return i;
            }
            else if (i > 64 && i < 91) // Upper case letters
            {
                return i;
            }
            else if (i > 96 && i < 123) // Lower case letters
            {
                return i;
            }
            else
            {
                return Char(); // Retry Random
            }
        }
        internal static bool Bool()
        {
            return Convert.ToBoolean(_rd.Next(0, 1));
        }
    }

 

posted @ 2020-10-26 14:14  devs  阅读(266)  评论(0编辑  收藏  举报