Create an integer that is completely impossible to repeat (in the description section of the updated formula)

Source: Internet
Author: User
For some readers who directly need results, I will publish the results at the very beginning (the current time is as follows ):
[ (Second * minute-second + millisecond) * hour-(Second * minute-second)] * day in the position of the Year-year
Note: day in the year = today is the day of the year

First, let me give an example ,. the random class in net provides the overloaded constructors, random () and random (INT). The latter parameter is a seed, and random uses this seed to generate pseudo-random numbers, theoretically, if the seed is different, random can generate rather random numbers if the parameter is null. net uses the current time as the seed.
At this time, if there is a method that can provide a large degree of different numbers, it can ensure that the number generated by random is quite random, of course. net has helped us with this. We can use the random () constructor, but we can use random to discuss the topic of this article.
. Ner uses the timeAlgorithmThis is a good solution. We can also refer to this method.

    1. First, we will discuss how to construct the random (datetime. now. second), test this object and you will find that 10 arrays with 10 different random numbers are generated, so the randomness is weak and 10 arrays are identical in one second, we can estimate the number of overlapped values if a continuous generation is performed for 10 minutes.
    2. In milliseconds, the meaning is similar to that in seconds. The interval between the generated random numbers is milliseconds, but the number of repeated milliseconds within 10 seconds can be thought, it is estimated that 10 minutes cannot meet the requirements.

likewise, it can be seen that the use of any time alone cannot meet the requirements. Therefore, a formula is required. The formula must be in a very small time unit and a very large time period without repeated results.
to explain the formula mentioned above, multiply the current second by the minute to obtain a number, for example, 12: 13 seconds, 4 minutes 4 seconds, 2 minutes 8 seconds, and 8 minutes 2 seconds. The results are and 16, respectively, 16, 16, the last three results are the same, this is minus the number of seconds, it becomes, 12, 6, 14, so the last three changes, using (seconds * minute-second) algorithm can eliminate the effect of the last three such as time. After adding the result in milliseconds, the generation interval of this number can be reduced to the unit of milliseconds.
likewise, the next copy will multiply by the hour in a similar way, and then subtract the hour to enlarge the time period. The day is unique in a year. Then, by multiplying it, we can get the unique number in a year. Then, after subtracting the year, we can exclude the time that may be the same in two years.
the problem with this algorithm is that although the results of each step are different in the previous calculation theory, the same results may be produced in the composite operation, but the variable in milliseconds exists, reduce this possibility to a negligible level.

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.