仅在设置断点时创建唯一键,否则复制键

时间:2015-11-09 21:04:28

标签: c#

所以,我确信有一个简单的解释,但我现在只使用C#几个月了,所以我还有很多东西需要学习。

我正在开发一个小应用程序,它会生成一组独特的键,只是一些基本的技能构建。

我遇到了这个问题,如果我在keyList.Add(sb.ToString());和F5设置了一个断点,通过所有迭代,我得到一个键列表,并且都有唯一的值,但如果我删除断点并运行解决它只是重复相同的密钥。

有人可以解释为什么会这样吗?是否与Random()StreamWriter的展示位置有关?

public class KeyCreator
{
    public static void alphaList()
    {
        string lowerAlpha = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
        string upperAlpha = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
        SortedList<string, string> alphaList = new SortedList<string, string>();
        string[] splitListLower = lowerAlpha.Split(',');
        string[] splitListUpper = upperAlpha.Split(',');
        for (int i = 0; i < splitListLower.Length; i++)
        {
            alphaList.Add(splitListLower[i], splitListUpper[i]);
        }
        numberGen(alphaList);
    }

    public static void numberGen(SortedList<string, string> alphaList)
    {
        List<string> keyList = new List<string>();
        for (int b = 0; b < 20; b++)
        {
            int max = alphaList.Count;
            StringBuilder sb = new StringBuilder();
            Random rnd = new Random();
            for (int i = 0; i < 4; i++)
            {
                int upperLower = rnd.Next(0, 10);
                if (upperLower < 5)
                {
                    for (int a = 0; a < 4; a++)
                    {
                        int lowerUpper = rnd.Next(0, 10);
                        if (lowerUpper < 4)
                        {
                            int index = rnd.Next(0, max);
                            sb.Append(alphaList.Keys[index]);
                        }
                        else if (lowerUpper > 3 && lowerUpper < 7)
                        {
                            int index = rnd.Next(0, max);
                            sb.Append(alphaList.Values[index]);
                        }
                        else if (lowerUpper > 6)
                        {
                            int rand = rnd.Next(0, 9);
                            sb.Append(rand);
                        }
                    }
                }
                else if (upperLower > 4)
                {
                    for (int a = 0; a < 4; a++)
                    {
                        int lowerUpper = rnd.Next(0, 10);
                        if (lowerUpper < 4)
                        {
                            int index = rnd.Next(0, max);
                            sb.Append(alphaList.Keys[index]);
                        }
                        else if (lowerUpper > 3 && lowerUpper < 7)
                        {
                            int index = rnd.Next(0, max);
                            sb.Append(alphaList.Values[index]);
                        }
                        else if (lowerUpper > 6)
                        {
                            int rand = rnd.Next(0, 9);
                            sb.Append(rand);
                        }
                    }
                }
                if (i < 3)
                {
                    sb.Append("-");
                }
            }
            keyList.Add(sb.ToString());
        }
        using (StreamWriter writer = new StreamWriter(@"C:\temp\keys.txt"))
        {
            foreach (string key in keyList)
            {
                writer.WriteLine(key);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:3)

问题在于:

public static void numberGen(SortedList<string, string> alphaList)
{
    List<string> keyList = new List<string>();
    for (int b = 0; b < 20; b++)
    {
        int max = alphaList.Count;
        StringBuilder sb = new StringBuilder();
        Random rnd = new Random();   //<-- This line is the problem
默认情况下,

Random以当前日期/时间播种。由于DateTime的准确性并不是很好,因此您反复创建相同的随机数。当您在调试器中中断时,它会停留足够长的时间,以使DateTime更改为Random创建新种子。

解决方案是这样做:

[ThreadStatic]
static Random rnd = new Random();
public static void numberGen(SortedList<string, string> alphaList)
{
    //This if is required because it can be null on subsequent threads.
    if (rnd == null) rnd = new Random();
    List<string> keyList = new List<string>();
    for (int b = 0; b < 20; b++)
    {
        int max = alphaList.Count;
        StringBuilder sb = new StringBuilder();

将创建移到循环外部并使用相同的实例。

另外,正如Scott Chamberlain在评论中指出的那样,Random不是线程安全的静态方法,所以我添加了[ThreadStatic] attribute以避免需要lock围绕每个Next电话。不执行此操作并从多个线程调用它可能会导致Random状态损坏并返回全零。