How do I quickly generate 1 million not-duplicate 8-bit numbers?

Source: Internet
Author: User

This problem Jiangjian a method (http://blog.csdn.net/zjcxc/archive/2006/08/20/1099215.aspx) and I give another method.

Suppose I want to produce 1 million random numbers that are not repeated between 10000000-99999999. You can create a table table1, which has two fields, one is the resulting random number segment (int), and the other is an int field.
Divide 1000000 = 89 with 99999999-10000000 = 89999999 (out). Then cycle from 10000000 to 1 million times, starting at 10000000 and adding 89 each time,
namely 10000000, 10000089, 10000178 ...
Then each cycle, using newid to generate a random number, the two numbers as a value into the Table1. That is, the first field inserts the number incremented by step 89, and the second number is the number randomly generated using NEWID. In this way, the Table1 has 1 million records, but the random values are sequential. However, because the second field is random, the value on the first field is random as long as the second field is sorted.

This ensures that 1 million random numbers without duplicates are guaranteed without checking for duplicate values. And they are evenly distributed between 10000000 and 99999999.
Of course, you can also randomly change the step size of 89 to make it more random.
This also has the benefit of generating records in batches and inserting them together in a table. If 1000 such records are generated at one time, the next 1000 records are generated after insertion. This can be done as long as the loop is 1000 times.

As for the code, very simple, write it yourself, here only give ideas.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.