100,000 numbers unordered, requires no repetition! Master in!!!

Source: Internet
Author: User
Tags shuffle
PS: Maybe I'm not very clear about what I'm describing, but that's what it means. build in turn10W random numbers are populated into arrays, between random number 1-10wcannot repeat。 Well, yes, that's what it meant. )

Today's interview, technical director gave me a study questions, ask random 10W numbers unordered arrangement, how to efficiently execute, memory not required, the best hardware configuration, pseudo-code as follows:

static $arr = array();for($i = 0; $i < 100000; $i++){    $rand = rand(1,100000);    $y = 0;    while($rand == $arr[$y])    {        $rand = rand(1,100000);        $y++;    }    $arr[$i] = $rand;}

The downside of this is that the probability of entering the while loop when the first number is started is 1/100000, then when the No. 99999 number is taken, the probability of entering the while loop is 99.9999%, then there will be an infinite loop state, then how to evade What about the problem?

Reply content:

PS: Maybe I'm not very clear about what I'm describing, but that's what it means. generates 10W random numbers in turn to populate the array with random number 1-10w , and cannot be duplicated . Well, yes, that's what it meant. )

Today's interview, technical director gave me a study questions, ask random 10W numbers unordered arrangement, how to efficiently execute, memory not required, the best hardware configuration, pseudo-code as follows:

static $arr = array();for($i = 0; $i < 100000; $i++){    $rand = rand(1,100000);    $y = 0;    while($rand == $arr[$y])    {        $rand = rand(1,100000);        $y++;    }    $arr[$i] = $rand;}

The downside of this is that the probability of entering the while loop when the first number is started is 1/100000, then when the No. 99999 number is taken, the probability of entering the while loop is 99.9999%, then there will be an infinite loop state, then how to evade What about the problem?

Update: Since the title of the main test instructions, I also updated here:

Generate 10W number, value range [1..10w] and no repetition, then only need to first in the length of 10W of the array filled with 1. 10W, then use the following algorithm shuffle on the line.

This can actually be achieved through the fisher-yates shuffle algorithm, which can achieve an O (n) of progressive complexity. The description is as follows:

void randomize(vector
  
   
    
   & data) {    for (int i = data.size() - 1; i > 0; --i) {        int random = rand() % (i + 1);  // 所以 0 <= random <= i         swap(data[random], data[i]);    }}
  
   

It is easy to prove the correctness of this algorithm. But the condition needs to be converted into an equivalent form, in which case we need to arrange the array randomly, which means that each number is equal to the probability of being in a seat, all 1/n (assuming n number)

We consider the time when the algorithm executes the first loop: we randomly pick a number from [0,n-1] This n number to place in the n-1, so all the numbers are placed in the n-1 probability of 1/n

When we execute the second time, we select a place from the remaining number of [0...n-2] n-1 to n-2, so what is the probability of appearing in the n-2 position? Note Now that this is not an independent event, a number to be put to n-2 here means that it is not selected in the first iteration and is selected for the second time, so the probability is (1-1/n) (1/(n-1)) = ((n-1)/N) (1 (n-1)) = 1/n, So the probability of all the numbers on N-2 is 1/n.

In general, when a number is placed at position I, it means that the front n-i-1 cycle is not selected, and in the first n-i is selected, we have the probability p (i) = (1-1/n) (1-1/(n-1)) (1-1/(n-2)) .... (1-1/(i+2)) (1/(i+1) = ((n-1)/N) ((n-2)/(N-1)) ((n-3)/(n-2)) ... ((i+1)/(n+2)) (1/(i+1)) = 1/n

Generate 10W numbers, increment randomly, and then shuffle the result set

Shuffle (range (0,100000));

Steps
1 generating 0-99999 contiguous array A
2 Empty array b
3 randomly takes one from a and puts it in B and removes the element from a until it's finished.

You may consider using format-preserving encryption

The general purpose is to format the same encryption, in this case, you can encrypt the 5-digit number to another 5-digit.

See the first reaction of the topic, really only I think of C + + STL random_suffle function ...
Later carefully read the title of the main use of PHP (why is the C tag ah ...). )
Okay, let's get a simplified version.
As a result, Dahakawang has been given the implementation, OK, I changed to PHP grammar Bar ~

$a=array();for($i=0;$i<100000;$i++) $a[$i]=$i+1;for($i=99999;$i>=0;$i--){    $j=rand(0,$i);    $t=$a[$j];    $a[$j]=$a[$i];    $a[$i]=$t;}

Direct handwriting, if there are grammatical errors to correct themselves.

"Random 10W numbers unordered" This does not really indicate a problem ...

If it is interpreted as "unordered arrangement of 100,000 random numbers", I think the code only needs one line: return.

Put 100,000 numbers in the array, and then the array is in random order. Well, I admit it's a test instructions.
The pseudo code below should be expected, but seems to be close to @Dappur answer.

$arr=range(1, 100000, 1);//shuffle($arr); 哈哈,这是玩笑$results = [];for($i=99999;$i>=0;$i--){    $key=rand(0,$i);    $results[]=$arr[$i];    array_slice($arr, $i, 1);}

Shuffle: Scrambled array order.
Range: Creates an array of intervals, with parameters of minimum, maximum, and step.
Array_slice: Take out a paragraph in the array and reset the subscript.

Re-@dappur
Want to understand your code ... The equivalent of 100,000 people, each holding a number, and then shouted a run, naturally disorderly.
And like the landlord said that one by one (is not predetermined well) in addition to his own writing that way is no solution, like 100,000 people are isolated, free to say a number, can not be repeated with others, impossible.

High efficiency means fast?
If it is, I will sequentially generate 10W number, 10W threads randomly inserted, if the location has been occupied with the nearest principle to find location inserted.
If there's a 10W nucleus, it's going to be pretty fast.

  • Related Article

    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.