Pick random char

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

i have some chars:

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

now i\'m lookin

相关标签:
11条回答
  • 2021-01-03 21:37

    Instead of 26 please use size of your CHARS buffer.

    int num = random.Next(0, chars.Length)
    

    Then instead of

    let = (char)('a' + num)
    

    use

    let = chars[num]
    
    0 讨论(0)
  • 2021-01-03 21:46

    You can try this :

     public static string GetPassword()
     {
    string Characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
     Random rnd = new Random();
    int index = rnd.Next(0,51);
    string char1 = Characters[index].ToString();
    return char1;
      }
    

    Now you can play with this code block as per your wish. Cheers!

    0 讨论(0)
  • 2021-01-03 21:51

    This might work for you:

    public static char GetLetter()
    {
        string chars = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&";
        Random rand = new Random();
        int num = rand.Next(0, chars.Length -1);
        return chars[num];
    }
    
    0 讨论(0)
  • 2021-01-03 21:51

    Getting Character from ASCII number:

    private string GenerateRandomString()
    {
    Random rnd = new Random();
    string txtRand = string.Empty;
    for (int i = 0; i <8; i++) txtRand += ((char)rnd.Next(97, 122)).ToString();
    return txtRand;
    }
    
    0 讨论(0)
  • 2021-01-03 21:53

    I wish This code helps you :

     string s = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&";
                Random random = new Random();
                int num = random.Next(0, s.Length -1);
                MessageBox.Show(s[num].ToString());
    
    0 讨论(0)
提交回复
热议问题