Pick random char

2019-02-12 07:11发布

i have some chars:

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

now i'm looking for a method to return a random char from these.

I found a code which maybe can be usefull:

static Random random = new Random();
        public static char GetLetter()
        {
            // This method returns a random lowercase letter
            // ... Between 'a' and 'z' inclusize.
            int num = random.Next(0, 26); // Zero to 25
            char let = (char)('a' + num);
            return let;
        }

this code returns me a random char form the alphabet but only returns me lower case letters

标签: c# random char
10条回答
相关推荐>>
2楼-- · 2019-02-12 08:10

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];
}
查看更多
forever°为你锁心
3楼-- · 2019-02-12 08:13

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());
查看更多
我只想做你的唯一
4楼-- · 2019-02-12 08:13

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;
}
查看更多
仙女界的扛把子
5楼-- · 2019-02-12 08:16

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!

查看更多
登录 后发表回答