How to quickly generate a random number of 1 million non-repeated 8-bit even distribution?

Source: Internet
Author: User
Original article: http://www.cnblogs.com/nokiaguy/archive/2008/05/11/1191906.html

10000000 non-repeated random numbers must be generated between 99999999 and 1 million. You can create a table named Table1, which has two fields: the random number field generated (INT type) and the int type field.

Use 99999999-10000000 = 89999999 division 1000000 = 89 (remove ). Then, 10000000 cycles start from 1000000, and 89 is added each time from 10000000,
10000000, 10000089, 10000178...
Then, a random number is generated using newid every cycle, and these two numbers are inserted into Table 1 as values. That is, the first field is inserted with an increment of 89 by step, and the second is the number randomly generated by newid. In this way, Table1 has 1 million records, but the random values are sequential. However, because the second field is random, the value of the first field is random as long as it is sorted by the second field.

In this way, you do not need to check the duplicate value to ensure that you can get 1000000 random numbers without duplicates. They are evenly distributed between 10000000 and 99999999.
Of course, you can also randomly change the 89 step to make it more random.
In this way, you can generate records in batches and insert them into the table. If 1000 such records are generated at a time, the next one thousand records will be generated after insertion. In this way, the task can be completed once every 1000 cycles.

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.