简体   繁体   English

长度为9的随机字符串碰撞

[英]Random string of length 9 hits collision

I want to generate a random string of length 9. 我想生成一个长度为9的随机字符串。

This is the code which hits collision about 10-15 times. 这是发生碰撞约10-15次的代码。 Credits to Random String Generator Returning Same String . 归功于返回相同字符串的随机字符串生成器 Can anybody help me to generate a truly random string? 有人可以帮助我生成真正的随机字符串吗?

  class Program
    {

        private static Random random = new Random((int)DateTime.Now.Ticks);
        private static object locker = new object();

        private static string RandomString(int size)
        {
            StringBuilder builder = new StringBuilder();
            char ch;
            for (int i = 0; i < size; i++)
            {
                lock (locker)
                {
                    ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                }
                builder.Append(ch);
            }

            return builder.ToString();
        }



        static void Main(string[] args)
        {            
            Dictionary<string, string> dict = new Dictionary<string, string>();
            object locker2 = new object();

            ThreadPool.QueueUserWorkItem(new WaitCallback((obj) => {
                for (int i = 0; i < 5000000; i++)
                {
                    string random = RandomString(9);
                    lock (locker2)
                    {
                        if (!dict.ContainsKey(random))
                            dict[random] = random;
                        else
                            Console.WriteLine("Found");
                    }

                }
            }));

            ThreadPool.QueueUserWorkItem(new WaitCallback((obj) =>
            {
                for (int i = 0; i < 5000000; i++)
                {
                    string random = RandomString(9);
                    lock (locker2)
                    {
                        if (!dict.ContainsKey(random))
                            dict[random] = random;
                        else
                            Console.WriteLine("Found");
                    }

                }
            }));

            ThreadPool.QueueUserWorkItem(new WaitCallback((obj) =>
            {
                for (int i = 0; i < 5000000; i++)
                {
                    string random = RandomString(9);
                    lock (locker2)
                    {
                        if (!dict.ContainsKey(random))
                            dict[random] = random;
                        else
                            Console.WriteLine("Found");
                    }

                }
            }));

            ThreadPool.QueueUserWorkItem(new WaitCallback((obj) =>
            {
                for (int i = 0; i < 5000000; i++)
                {
                    string random = RandomString(9);
                    lock (locker2)
                    {
                        if (!dict.ContainsKey(random))
                            dict[random] = random;
                        else
                            Console.WriteLine("Found");
                    }

                }
            }));

            Console.ReadKey();
        }
    }

Even using a perfectly random string without constraints, you'll likely get collisions once you generate around 2 million entries. 即使使用不受限制的完全随机的字符串,一旦生成大约200万个条目,您也很可能会发生冲突。 There are 26^9 total strings. 总共有26 ^ 9个字符串。 Collisions become likely once you hit around the square root of that, which is around 2.3 million. 一旦碰到大约230万的平方根,就会有可能发生冲突。 Check out the Birthday problem . 检查一下生日问题

You have a couple of choices: 您有两种选择:

  • Increase the number of possible strings significantly. 显着增加可能的字符串数。 This means a longer string, and possibly more characters 这意味着更长的字符串,可能还有更多的字符
  • Keep track of existing values, and reject them. 跟踪现有值,并拒绝它们。
  • Use a counter and pass it to a pseudo random permutation of the desired size. 使用计数器,并将其传递给所需大小的伪随机排列。

It might perform as well, but if you need a truly random number you should use the RandomNumberGenerator class. 它的性能可能也不错,但是如果您需要一个真正的随机数,则应该使用RandomNumberGenerator类。 This gives you a crypto-random number and will do a better job distributing the random-ness. 这为您提供了一个加密随机数,并且可以更好地分配随机性。 This, of course, only really matters if you need crypto-random strings. 当然,这仅在需要加密随机字符串时才真正重要。 This SO question does a good job at discussing the difference. 这样的问题很好地讨论了差异。

使用GUID并将其压缩为您喜欢的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM