Solution to repeated random functions in C # Asp.net

Source: Internet
Author: User

A few days ago, when I was working on a dating site (www.niyuewo.com), I encountered a duplicate member number problem. After searching for information on the Internet, I found it was a problem with the system timestamp, So I sorted it out, hope to help you

In C #, the random function calls the next method in system. Random to generate a random number. First, define a Random Seed, for example:
Int R; here, an initial value is usually assigned, and then the new random (r); R. next (INT macvalue) -- Note: The range of random numbers can be specified in brackets of the next method. Its usage is R. next (1,100) -- in 1 ~ A random positive integer is generated in 100.
In fact, if the above method is applied to Windows FormsProgramA bug occurs in the Click Event of the button in, that is, the random number is generated when the Click Event of the button is triggered for the first time. However, when you click the button for the second time, but it produces the same number as the previous one. How can this bug be solved? We need to use a system function datatime. Now. millisecond. Because the current time of the system will not be repeated, we can declare it as follows:
Int RA;
Random RD = new random (datetime. Now. millisecond );
Ra = RD. Next (1,100 );
In this way, the problem is solved successfully!

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.