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.
Select top 10
Rand ()
From sysobjects
3. In efficiency control, use the cycle + batch generation method instead of the traditional one-by-one generation method. In SQL Server, each insert statement has an internal transaction. If one is inserted one by one, the overhead of the transaction is too large and the efficiency is bound to be very low. Instead of generating 1 million data at a time, first, because the generated data may be duplicated, there will be no more than 1 million if the duplicate is removed. Second, 1 million data is generated at a time, and the memory and CPU resources consumed are also high. Generally, the computer may not be able to withstand this problem.
Another example:
Generate random numbers not repeated 1. C # how to generate random numbers not repeated within 100
You can consider placing these 100 numbers in a single number group and randomly taking a location (the first time is 1-100, the second time is 1-99 ,...), you can compare the number of the position with the number of the last position.
Using system;
Using system. Collections. Generic;
Using system. text;
Namespace consoleapplication1
{
Class Program
{
Static void main (string [] ARGs)
{
Int [] arr = new int [101];
Int I;
// Initialize the Array
For (I = 1; I <= 100; I ++)
{
Arr [I] = I;
}
// Random Number
Random r = new random ();
For (Int J = 100; j> = 1; j --)
{
Int address = R. Next (1, J );
Int TMP = arr [address];
Arr [address] = arr [J];
Arr [J] = TMP;
}
// Output
Foreach (int K in ARR)
{
Console. Write (K + "");
}
}
}
}
2, C # generate random numbers that are not repeated
/// <Summary>
/// Function Description: returns an array of non-repeated random numbers.
/// </Summary>
/// <Param name = "num"> Number of random numbers </param>
/// <Param name = "minnum"> lower limit of random number </param>
/// <Param name = "maxnum"> maximum random number </param>
/// <Returns> </returns>
Public int [] getrandomarray (INT number, int minnum, int maxnum)
{
Int J;
Int [] B = new int [number];
Random r = new random ();
For (j = 0; j <number; j ++)
{
Int I = R. Next (minnum, maxnum );
Int num = 0;
For (int K = 0; k <j; k ++)
{
If (B [k] = I)
{
Num = num + 1;
}
}
If (num = 0)
{
B [J] = I;
}
Else
{
J = J-1;
}
}
Return B;
}