Pick random char

2019-02-12 07:37发布

问题:

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

回答1:

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).



回答2:

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];
}


回答3:

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.



回答4:

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);
}


回答5:

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]


回答6:

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();
        }


回答7:

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());


回答8:

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;
}


回答9:

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!



回答10:

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];


标签: c# random char