Pick random char

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

i have some chars:

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

now i\'m lookin

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

    Your Code is good, you just need to change 'a' to 'A'

    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 same as mentioned in the question , just change char let = (char)('A' + num); , it returns upper case letters.

    Thanks!!!

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

    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);
    }
    
    0 讨论(0)
  • 2021-01-03 21:29
    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();
            }
    
    0 讨论(0)
  • 2021-01-03 21:31

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

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

    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.

    0 讨论(0)
  • 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];
    
    0 讨论(0)
提交回复
热议问题