NET random random number duplication problem

Source: Internet
Author: User

In the actual project not only need to randomly generate the password string, but also to generate more than one at a time. I put the method of generating the random string into the for loop, and the problem arises.

The resulting string is duplicated.

After multiparty verification, the reason in the code.

// use seed associated with the system time New Random ();

The problem is that the system time is updated at 10ms, and the For loop is much shorter than 10ms, so there will be duplicates.

How to fix it, of course, is not using the default seed, manually specified, and the seed cannot be duplicated . I think the counter in the For loop is right.

 for (int0; i < count; i++) {     // Take I as seed, generate random number     new  Random (i);}

Perfect solution.

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.