100 million non-repeating random number algorithms

Source: Internet
Author: User

Recently browsed the "Programmer forum" found a lot of good posts, and grew a lot of knowledge, now take one of them as an example to share the experience with you.

Someone raises a question: how can I generate 100 million random numbers that don't repeat?

The problem is simple, and it seems as if figuring out what random numbers are and how to generate random numbers with a computer can solve the problem.

Random number, the individual understood as a certain range of irregular numbers, such as throwing a dice, falling on the table on the top side of the number is a random number, this number can only be in the range of 1 to 6, but specifically what number, who is not sure, because it has no regularity. A set of random numbers, to throw the dice is thrown out six different numbers, such as washing a poker, after washing is 54 non-repeating random number.

Second question, how to generate random numbers with a computer? Just call a function of a language. In fact, the computer is not able to generate a real random number, because the computer is a highly regular machine, let it generate an irregular number, simply do not. Usually the programmer uses a function to generate a random number, just use an algorithm to get out of the pseudo-random number, looks like, actually not, can solve the problem on the line.

Go back to the problem described in this post. Generates 100 million non-repeating random numbers, the most straightforward algorithm is to generate a number per function, put it in a basket, the first number directly into the basket, the number of subsequent generation in the basket before and in the basket of each number of comparison, once found in the basket and the number of new generation of the same number, lost the number of the new generation, And then generate the number.

Without a doubt, the efficiency of this algorithm is very low, look at the number of comparisons to know that the worst times tend to infinity. That is, until later, it is almost impossible to generate a different number than before.

Of course, this algorithm can be upgraded to a much more efficient algorithm, each generation of a number, the number from the random number generator to remove the range, such as to generate 10 random numbers, the first generation of a 3, I took 3 from the range of random numbers, the second time only from 1 to 9 of the range to find. 3 corresponds to 4, 4 corresponds to 5 ... 9 corresponds to 10. So there is no comparison of the link, but there is a corresponding link, each generation of a number after the remaining number will be re-corresponding again, efficiency is not optimistic.

At present, I represent the ordinary programmer's imagination to this end, can not think of any high-level solution, when throwing a brick out, the following will be the real Jasper-mathematician Programmer's algorithm grand Introduction please come out.

Let's start with another look at random numbers that don't repeat: encryption. The order in which an English string can be read is scrambled, and it becomes unreadable, which is encryption. But it has to be scrambled regularly, the letter a corresponds to another fixed letter ax, the letter B corresponds to another fixed letter bx, and so on, and must be one by one corresponding. Then the string "Ab...z" the 26 letters corresponding to the 26 encryption letter "AXBX and ZX" can be regarded as the corresponding range A to Z of the non-repeating pseudo-random number, which is the source of the mathematician's algorithm.

Look at the original text of the person replying:

"A 32bit RSA algorithm can be used to set A from 2~ (N-1) c= (a EXP D)  mod N satisfies the following conditions: D is a prime number, N is the product of two primes (p,q), (D * E) mod ((P-1) * (Q-1)) =1 because:   If c= (a exp D) mod n is: a= (c exp E) mod n So, C corresponds to A one by one. Therefore, for a=2~ (N-1), there is no repetition, no omission of pseudo-random code C. ”

Usually a little bit of math, especially the problem of higher mathematics, I am waiting for the general appearance of a bit of difficulty, although the text is not long, but also to take a slow look.

The RSA algorithm is one of the three Algorithms of Cryptography (RSA, MD5, DES), which is an asymmetric cipher algorithm. Say that if the condition is satisfied: D is a prime number, N is the product of two prime numbers (P,Q), (d * E) mod ((P-1) * (Q-1)) = 1, then there is C and A (range from 2 to N-1) one by one corresponds, and c= (a EXP D) mod N. A is a sequential number, and C is a pseudo-random number that appears to be irregular. The MoD operation represents a modulo, such as 7mod3=1. It means 7 divided by 3 + 1. Similar to 8mod3=2,9mod3=0. EXP represents the number of times before the preceding number, and AEXPD represents the D-square of a. The two operations are clear, and the others are no more difficult, * is the meaning of multiplication, most science students are clear.

Search the network, but also to add some conditions, 1,p and Q can not be the same. 2,e< (P-1) (Q-1) and the largest Q-1 of E and (P-1) (common factor) are 1.

Let's experiment with an example to see how amazing the algorithm is.

Set N=15,p=5,q=3, then A is a number from 2 to 14. Now you want to generate a pseudo-random number from 2 to 14. Take D to 3,e for 3,

C2= (2EXP3) mod15 = 8,

c3= (3EXP3) mod 15 = 12,

C4 = (4EXP3) mod 15 = 4,

C5 = (5EXP3) mod 15 = 5,

C6 = (6EXP3) mod 15 = 6,

C7 = (7EXP3) mod 15 = 13,

C8 = (8EXP3) mod 15 = 2,

C9 = (9EXP3) MoD 15 = 9,

C10 = (10EXP3) mod 15 = 10,

C11 = (11EXP3) mod 15 = 11,

C12 = (12EXP3) mod 15 = 3,

C13 = (13EXP3) mod 15 = 7,

C14 = (14EXP3) mod 15 = 14.

More perfect, if the number is a little larger, it may seem more random.

The 100 million pseudo-random number generated by this algorithm, the efficiency that is quite high, but the operation of a large number of operations to use the database. In some of the efficiency of the application, then do some correspondence on the processing, upgrade the algorithm, it will be quite perfect.

It can be seen that the optimization of the algorithm, if only to stay in the brain can imagine the primary mathematics stage, it is far from the requirements. A good programmer, but also need to deepen the understanding of discrete mathematics, although the proposed algorithm has been deep into the level of number theory, but the RSA algorithm is a very broad application of the algorithm, to its slightly flexible, it can play a more magical role. Programmers still need to learn more algorithms, to learn more mathematics, to play out beyond the general programmer's incredible ability.

100 million non-repeating random number algorithms

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.