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 07:50

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);
}
查看更多
看我几分像从前
3楼-- · 2019-02-12 07:53

Well you're nearly there - you want to return a random element from a string, so you just generate a random number in the range of the length of the string:

public static char GetRandomCharacter(string text, Random rng)
{
    int index = rng.Next(text.Length);
    return text[index];
}

I'd advise against using a static variable of type Random without any locking, by the way - Random isn't thread-safe. See my article on random numbers for more details (and workarounds).

查看更多
SAY GOODBYE
4楼-- · 2019-02-12 08:00

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]
查看更多
Animai°情兽
5楼-- · 2019-02-12 08:03

You can use it like;

char[] chars = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&".ToCharArray();
Random r = new Random();
int i = r.Next(chars.Length);
Console.WriteLine(chars[i]);

Here is a DEMO.

查看更多
Juvenile、少年°
6楼-- · 2019-02-12 08:04
private static void Main(string[] args)
        {
            Console.WriteLine(GetLetters(6));
            Console.ReadLine();
        }

        public static string GetLetters(int numberOfCharsToGenerate)
        {
            var random = new Random();
            char[] chars = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&".ToCharArray();

            var sb = new StringBuilder();
            for (int i = 0; i < numberOfCharsToGenerate; i++)
            {
                int num = random.Next(0, chars.Length);
                sb.Append(chars[num]);
            }
            return sb.ToString();
        }
查看更多
Anthone
7楼-- · 2019-02-12 08:06

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];
查看更多
登录 后发表回答