C # solutions for random character string randomness deficiency (random number repetition)

Source: Internet
Author: User

This is actually not a problem. In general, there will be no problems. The Code is as follows:

Private string random_str (INT length)
{

Int number;
String checkcode = string. empty;
Int iseed = 10;
Random RO = new random (10 );
Long tick = datetime. Now. ticks;
Random random = new random (INT) (tick & 0 xffffffffl) | (INT) (tick> 32 ));
For (INT I = 0; I <length; I ++)
{
Number = random. Next ();
Number = Number % 36;
If (number <10)
{
Number + = 48;
}
Else
{
Number + = 55;
}
Checkcode + = (char) Number). tostring ();
}
Return checkcode;
}

But if you have a stomachache, this will not work.

Random_str (4) + random_str (4) + random_str (4)

 

You will generate 12 new characters which are actually three identical strings, so there are countless Trojans in your mind.

In fact, adding a sleep will solve this problem:

 

Private string random_str (INT length)
{
System. Threading. thread. Sleep (1 );
Int number;
String checkcode = string. empty;
Int iseed = 10;
Random RO = new random (10 );
Long tick = datetime. Now. ticks;
Random random = new random (INT) (tick & 0 xffffffffl) | (INT) (tick> 32 ));
For (INT I = 0; I <length; I ++)
{
Number = random. Next ();
Number = Number % 36;
If (number <10)
{
Number + = 48;
}
Else
{
Number + = 55;
}
Checkcode + = (char) Number). tostring ();
}
Return checkcode;
}

Essentially closer to real random

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.