Pick random char

前端 未结 11 1454
名媛妹妹
名媛妹妹 2021-01-03 20:57

i have some chars:

chars = \"$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&\".ToCharArray();

now i\'m lookin

11条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-03 21:29

    I had approximate issue and I did it by this way:

    public static String GetRandomString()
    {
        var allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
        var length = 15;
    
        var chars = new char[length];
        var rd = new Random();
    
        for (var i = 0; i < length; i++)
        {
            chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
        }
    
        return new String(chars);
    }
    

提交回复
热议问题