How to generate a random string, and specify the length you want, or better generate unique string on specification you want

前端 未结 11 2324
一生所求
一生所求 2020-12-24 06:24

There is a library to generate Random numbers, so why isn\'t there a library for generation of random strings?

In other words how to generate a random string, and sp

相关标签:
11条回答
  • 2020-12-24 07:15

    How about

    string.Join("", Enumerable.Repeat(0, 100).Select(n => (char)new Random().Next(127)));
    

    or

    var rand = new Random();
    string.Join("", Enumerable.Repeat(0, 100).Select(n => (char)rand.Next(127)));
    
    0 讨论(0)
  • 2020-12-24 07:15
    public  static String getAlphaNumericString(int n) {
    
            String AlphaNumericString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789" + "abcdefghijklmnopqrstuvxyz";
    
            StringBuilder sb = new StringBuilder(n);
    
            for (int i = 0; i < n; i++) {
    
                // generate a random number between
                // 0 to AlphaNumericString variable length
                int index = (int) (AlphaNumericString.length() * Math.random());
    
                // add Character one by one in end of sb
                sb.append(AlphaNumericString.charAt(index));
            }
    
            return sb.toString();
        }
    
    0 讨论(0)
  • 2020-12-24 07:19

    How about-

        static Random rd = new Random();
        internal static string CreateString(int stringLength)
        {
            const string allowedChars = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz0123456789!@$?_-";
            char[] chars = new char[stringLength];
    
            for (int i = 0; i < stringLength; i++)
            {
                chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
            }
    
            return new string(chars);
        }
    
    0 讨论(0)
  • 2020-12-24 07:24

    Sometimes a random string can be useful in something like a unit test, if it was me I would add the AutoFixture nuget package and then do something like this. In my example I need a 121 character string...

            var fixture = new Fixture();
            var chars = fixture.CreateMany<char>(121).ToArray();
            var myString = new string(chars);
    

    If this wasn't in a unit test then I'd create myself a nuget package and use Autofixture in that, or something else if I needed only certain character types.

    0 讨论(0)
  • 2020-12-24 07:27

    I can't recall where I got this, so if you know who originally authored this, please help me give attribution.

    private static void Main()
    {
        const string AllowedChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#@$^*()";
        Random rng = new Random();
    
        foreach (string randomString in rng.NextStrings(AllowedChars, (15, 64), 25))
        {
            Console.WriteLine(randomString);
        }
    
        Console.ReadLine();
    }
    
    private static IEnumerable<string> NextStrings(
        this Random rnd,
        string allowedChars,
        (int Min, int Max)length,
        int count)
    {
        ISet<string> usedRandomStrings = new HashSet<string>();
        (int min, int max) = length;
        char[] chars = new char[max];
        int setLength = allowedChars.Length;
    
        while (count-- > 0)
        {
            int stringLength = rnd.Next(min, max + 1);
    
            for (int i = 0; i < stringLength; ++i)
            {
                chars[i] = allowedChars[rnd.Next(setLength)];
            }
    
            string randomString = new string(chars, 0, stringLength);
    
            if (usedRandomStrings.Add(randomString))
            {
                yield return randomString;
            }
            else
            {
                count++;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题