Not much to say, directly on the code test, before the online to see a generated random array, write really not how, the following is my way, there is a better way to communicate
C Code: Soon and quickly
#include <stdio.h> #include <stdlib.h> #include <time.h>void main () { long last = 1000000; Long *arr= (Long) malloc (last*sizeof (long)); Long *brr= (Long) malloc (last*sizeof (long)); Long I,r; Srand ((unsigned) time (NULL)); for (i=0;i<last;i++) { arr[i]=i; } while (last>0) { r=rand () *rand ()%1000000; if (arr[r]==-10) continue; BRR[1000000-LAST]=ARR[R]; arr[r]=-10; last--; } printf ("%d\n", Brr[0]); printf ("%d\n", last);}
PHP Code: The own function, in fact, is the implementation of the C shuffle 22 random switching principle (fast sorting is also two exchanges, so the space complexity must be a little lower)
<?php$arr = Range (0, 1000000), $length = 1000001;shuffle ($arr), echo $arr [2];echo "\ n"; echo Xdebug_time_index ();//end
PHP Code: The implementation of their own, not the general slow to 17s or so, is already very fast one way, the other way will generally show that the memory is exhausted
<?php$arr = Range (0, 1000000), $length = 1000001, $out = Array (), while ($length) { $R = Mt_rand (0, $length); $out [] = $arr [$R]; $arr [$R]= $arr [$length-1]; $length--;} Shuffle ($arr); Echo $arr [2];echo "\ n"; echo Xdebug_time_index ();//end
Generating a random array