How to quickly generate 1 million random 8-bit numbers without duplicates?

Source: Internet
Author: User
This article is original. If you need to reprint it, please indicate the author and the source. Thank you!

Suppose I want to generate 10000000 non-repeated random numbers 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...
However
After each cycle, use newid to generate a random number and insert the two numbers into Table 1 as values. That is to say, the first field is inserted with an increment of 89 by step, and the second number is randomly inserted using newid.
The number of instances generated. 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.

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.

AsCodeIt's very easy. Write it by yourself. Here we only provide ideas !!

Related Article

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.