Pick random char

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

i have some chars:

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

now i\'m lookin

11条回答
  •  温柔的废话
    2021-01-03 21:33

    I'm not sure how efficient it is as I'm very new to coding, however, why not just utilize the random number your already creating? Wouldn't this "randomize" an uppercase char as well?

        int num = random.Next(0,26);           
        char let = (num > 13) ? Char.ToUpper((char)('a' + num)) : (char)('a' + num);
    

    Also, if you're looking to take a single letter from your char[], would it be easier to just use a string?

        string charRepo = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&"; 
        Random rando = new Random();
        int ranNum = rando.Next(0, charRepo.Length);
    
        char ranChar = charRepo[ranNum];
    

提交回复
热议问题