A random number of C # solves the random number duplication problem.

Source: Internet
Author: User
Tags random seed
By default,. Net random numbers are generated based on the system time. If the computer speed is very fast, the random numbers will be the same.
Random RND = new random ();
Int rndnum = RND. Next (); // random number within the int value range
Int rndnum = RND. Next (10); // get 0 ~ Random Number of 9
Int rndnum = RND. Next (10, 20); // get 10 ~ Random Number of 19
Int rndnum = RND. nextdouble (); // 0 ~ Random Number of 1 If the random seed is the system time, multiple random numbers are generated at a time in a loop.
Because the CPU operation speed is too fast, every time you get the same time, the generated numbers are all the same.
So we need to constantly change the seeds.
Public String getrandomcode ()
{
Char [] chars = {
'A', 'B', 'C', 'D', 'E', 'E', 'F', 'G', 'h', 'J', 'k ', 'M', 'n', 'P', 'Q', 'R','s ',
'T', 'U', 'V', 'w', 'x', 'y', 'z', '2', '3', '4 ', '5', '6', '7', '8', '9'
};

String code = string. empty;

For (INT I = 0; I <4; I ++)
{

// The key here is to pass in a seed parameter to ensure that the generated random number is different.
// Random RND = new random (unchecked (INT) datetime. Now. ticks ));
Random RND = new
Random (getrandomseed ());
Code + = chars [RND. Next (0, 30)]. tostring ();
}

Return code;
}

/// <Summary>
/// Encrypt the random number generator to generate a Random Seed
/// </Summary>
/// <Returns> </returns>

Staticint getrandomseed ()

{

Byte [] bytes =
Newbyte [4];

System. Security. cryptography. rngcryptoserviceprovider RNG = new system. Security. cryptography. rngcryptoserviceprovider ();

RNG. getbytes (bytes );

Returnbitconverter. toint32 (bytes, 0 );

}

Get the idea of shuffling a specified number of random combinations

Public static ilist <string> createchargecodeno (string promotionchargecodeno, int count)

{

List <string> Lis = new list <string> ();

If (string. isnullorempty (promotionchargecodeno ))

{

Return Lis;

}

String chargecodeno = promotionchargecodeno;

Int length = 10-promotionchargecodeno. length;

While (LIS. Count <count)

{

Int [] numbers = new int [length * 2];

For (INT I = 0; I <length * 2; I ++)

Numbers [I] = I + 1;

For (INT I = 0; I <length * 2; I ++) // shuffled

{

Random Rand = new random (getrandomseed ());

Int temp = Rand. Next (length * 2 );

Int tempnumber = numbers [I];

Numbers [I] = numbers [temp];

Numbers [temp] = tempnumber;

}

String code = "";

For (INT x = 0; Code. Length <length; X ++)

{

Code + = numbers [x];

}

Code = code. substring (0, length );

String S = chargecodeno + code;

If (LIS. Contains (s ))

{

Continue;

}

Lis. Add (chargecodeno + Code );

}

Return Lis;

}

 

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.