Random Weight to 1 million non-repeated random Encoding

Source: Internet
Author: User
Tags rowcount

Zjcxc

Recently, someone asked me in the forum how to quickly generate an 8-bit number that is not repeated in 1 million. There are several points to note for this question:
1. How to generate an 8-bit random number. The more random the number is, the less likely it is to be repeated.
2. Control is not repeated
3. Consider Performance
To solve this problem, I wrote the following example, hoping to provide guidance for those who have such requirements.

Example of generating 1 million pieces of 8-bit non-duplicated data

Use tempdb
Go

-- Create a test table

Create Table Tb (ID char (8 ))

-- Create a unique index used to automatically filter duplicate values

Create unique index ix_tb on TB (ID)
With ignore_dup_key
Go

-- Test the processing time of data insertion and record the start time of processing

Declare @ DT datetime
Set @ dt = getdate ()
-- Insert random data

Set nocount on

Declare @ row int
Set @ ROW = 1000000 -- set the total number of records

While @ row> 0
Begin
-- Displays a message indicating that multiple rows of data need to be inserted.
Raiserror ('need % d rows ', 10, 1, @ row) with Nowait
-- Insert random bit encoding data

Set rowcount @ row

Insert TB select
Id = right (100000000 + convert (bigint, ABS (checksum (newid (), 8)
From syscolumns C1, syscolumns C2

Set @ ROW = @ row-@ rowcount
End

-- Display the time when data is inserted
Select begindate = @ DT, enddate = getdate (),
Second = datediff (second, @ DT, getdate ()),
Go
-- Display whether the final result record is correct
Select count (*) from TB
Go
-- Delete test
Drop table TB

Tips used in solution:

1. control that the generated data is not duplicated. directly use the ignore_dup_key option in the unique index to automatically filter duplicate values in the inserted data to avoid manual duplication.

2. use checksum with the newid () function to make the generated data as random as possible. Generally, the Rand () function is used to generate a random number, but this function generates a pseudo random value, test with the following statement and you will find that all the generated data is the same. This is not suitable for generating multiple random numbers in batches, while the newid () function generates guid, basically, there will be no duplicates, and then convert them into numbers through checksum, which makes it less likely to generate duplicates.

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.