Generate a random permutation of elements from range [ L , R ] ( Divide and Conquer ) | To store the random permutation ; Utility function to print the generated permutation ; Function to return a random number between x and y ; Recursive function to generate the random permutation ; Base condition ; Random number returned from the function ; Inserting random number in vector ; Recursion call for [ l , n - 1 ] ; Recursion call for [ n + 1 , r ] ; Driver code ; Generate permutation ; Print the generated permutation
< ? php $ permutation = array ( ) ; function printPermutation ( ) { global $ permutation ; foreach ( $ permutation as $ i ) echo $ i . " " ; } function give_random_number ( $ l , $ r ) { $ x = rand ( ) % ( $ r - $ l + 1 ) + $ l ; return $ x ; } function generate_random_permutation ( $ l , $ r ) { global $ permutation ; if ( $ l > $ r ) return ; $ n = give_random_number ( $ l , $ r ) ; array_push ( $ permutation , $ n ) ; generate_random_permutation ( $ l , $ n - 1 ) ; generate_random_permutation ( $ n + 1 , $ r ) ; } $ l = 5 ; $ r = 15 ; generate_random_permutation ( $ l , $ r ) ; printPermutation ( ) ; ? >