Performance issue with generation of random unique numbers

前端 未结 10 531
星月不相逢
星月不相逢 2021-01-17 13:47

I have a situation where by I need to create tens of thousands of unique numbers. However these numbers must be 9 digits and cannot contain any 0\'s. My current approach is

10条回答
  •  星月不相逢
    2021-01-17 14:29

    use string array or stringbuilder, wjile working with string additions.

    more over, your code is not efficient because after generating many id's your list may hold new generated id, so that the while loop will run more than you need.

    use for loops and generate your id's from this loop without randomizing. if random id's are required, use again for loops and generate more than you need and give an generation interval, and selected from this list randomly how much you need.

    use the code below to have a static list and fill it at starting your program. i will add later a second code to generate random id list. [i'm a little busy]

        public static Random RANDOM = new Random();
        public static List randomNumbers = new List();
        public static List randomStrings = new List();
    
        private void fillRandomNumbers()
        {
            int i = 100;
            while (i < 1000)
            {
                if (i.ToString().Contains('0') == false)
                {
                    randomNumbers.Add(i);
                }
            }
        }
    

提交回复
热议问题