{{ message }}

Instantly share code, notes, and snippets.

# ashfame/randomize_preferred_nodes_each_node.php

Created May 3, 2015
Simple algorithm for picking up 3-7 preferred nodes for each node among a pool of nodes, so that its a little uneven (every node has different number of preferred nodes) but overall there is a balance in distribution.
 \$nodes = array(); for ( \$i = 1; \$i <= 100; \$i++ ) { \$nodes[ \$i ] = ''; } print_r( \$nodes ); foreach ( \$nodes as \$key => &\$node ) { \$random_keys_selection = array_rand( \$nodes, mt_rand( 3, 7 ) ); var_dump( \$random_keys_selection ); \$node[ \$key ] = implode( ',', \$random_keys_selection ); } print_r( \$nodes );

### ashfame commented May 3, 2015

 ``````Array (  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  =>  => ) array (size=3) 0 => int 57 1 => int 62 2 => int 75 array (size=5) 0 => int 25 1 => int 40 2 => int 59 3 => int 70 4 => int 71 array (size=7) 0 => int 24 1 => int 34 2 => int 48 3 => int 52 4 => int 55 5 => int 71 6 => int 94 array (size=7) 0 => int 10 1 => int 14 2 => int 19 3 => int 57 4 => int 78 5 => int 82 6 => int 99 array (size=4) 0 => int 3 1 => int 4 2 => int 61 3 => int 84 array (size=6) 0 => int 9 1 => int 15 2 => int 17 3 => int 30 4 => int 71 5 => int 94 array (size=7) 0 => int 4 1 => int 7 2 => int 17 3 => int 33 4 => int 34 5 => int 70 6 => int 86 array (size=4) 0 => int 1 1 => int 28 2 => int 73 3 => int 84 array (size=5) 0 => int 53 1 => int 61 2 => int 73 3 => int 77 4 => int 100 array (size=6) 0 => int 11 1 => int 15 2 => int 20 3 => int 38 4 => int 81 5 => int 89 array (size=6) 0 => int 52 1 => int 56 2 => int 73 3 => int 78 4 => int 86 5 => int 99 array (size=5) 0 => int 37 1 => int 46 2 => int 68 3 => int 75 4 => int 81 array (size=4) 0 => int 26 1 => int 49 2 => int 91 3 => int 92 array (size=7) 0 => int 9 1 => int 21 2 => int 55 3 => int 80 4 => int 89 5 => int 90 6 => int 97 array (size=6) 0 => int 6 1 => int 10 2 => int 27 3 => int 40 4 => int 48 5 => int 53 array (size=7) 0 => int 4 1 => int 8 2 => int 58 3 => int 65 4 => int 83 5 => int 97 6 => int 98 array (size=6) 0 => int 9 1 => int 36 2 => int 37 3 => int 40 4 => int 62 5 => int 89 array (size=3) 0 => int 20 1 => int 32 2 => int 76 array (size=4) 0 => int 25 1 => int 39 2 => int 43 3 => int 54 array (size=5) 0 => int 8 1 => int 46 2 => int 47 3 => int 58 4 => int 96 array (size=5) 0 => int 29 1 => int 36 2 => int 58 3 => int 68 4 => int 73 array (size=6) 0 => int 3 1 => int 6 2 => int 11 3 => int 16 4 => int 21 5 => int 53 array (size=7) 0 => int 9 1 => int 10 2 => int 80 3 => int 91 4 => int 93 5 => int 96 6 => int 97 array (size=3) 0 => int 1 1 => int 29 2 => int 46 array (size=6) 0 => int 11 1 => int 15 2 => int 20 3 => int 35 4 => int 58 5 => int 99 array (size=3) 0 => int 69 1 => int 72 2 => int 83 array (size=5) 0 => int 37 1 => int 44 2 => int 49 3 => int 64 4 => int 66 array (size=4) 0 => int 15 1 => int 43 2 => int 47 3 => int 94 array (size=3) 0 => int 7 1 => int 71 2 => int 82 array (size=3) 0 => int 4 1 => int 69 2 => int 74 array (size=6) 0 => int 16 1 => int 23 2 => int 42 3 => int 44 4 => int 60 5 => int 83 array (size=7) 0 => int 6 1 => int 17 2 => int 31 3 => int 41 4 => int 60 5 => int 65 6 => int 87 array (size=7) 0 => int 12 1 => int 28 2 => int 34 3 => int 41 4 => int 46 5 => int 53 6 => int 62 array (size=3) 0 => int 3 1 => int 15 2 => int 100 array (size=3) 0 => int 37 1 => int 54 2 => int 89 array (size=4) 0 => int 14 1 => int 41 2 => int 51 3 => int 59 array (size=7) 0 => int 19 1 => int 21 2 => int 40 3 => int 48 4 => int 53 5 => int 73 6 => int 83 array (size=6) 0 => int 15 1 => int 37 2 => int 56 3 => int 60 4 => int 65 5 => int 97 array (size=5) 0 => int 23 1 => int 40 2 => int 59 3 => int 92 4 => int 97 array (size=4) 0 => int 24 1 => int 51 2 => int 68 3 => int 80 array (size=3) 0 => int 11 1 => int 52 2 => int 81 array (size=5) 0 => int 16 1 => int 18 2 => int 33 3 => int 40 4 => int 44 array (size=6) 0 => int 28 1 => int 31 2 => int 41 3 => int 61 4 => int 72 5 => int 87 array (size=3) 0 => int 2 1 => int 5 2 => int 68 array (size=7) 0 => int 31 1 => int 32 2 => int 33 3 => int 72 4 => int 78 5 => int 87 6 => int 97 array (size=6) 0 => int 5 1 => int 18 2 => int 39 3 => int 88 4 => int 96 5 => int 97 array (size=4) 0 => int 1 1 => int 40 2 => int 56 3 => int 93 array (size=7) 0 => int 6 1 => int 15 2 => int 30 3 => int 58 4 => int 65 5 => int 72 6 => int 79 array (size=5) 0 => int 5 1 => int 10 2 => int 43 3 => int 62 4 => int 88 array (size=5) 0 => int 49 1 => int 53 2 => int 64 3 => int 75 4 => int 86 array (size=6) 0 => int 8 1 => int 37 2 => int 55 3 => int 66 4 => int 74 5 => int 82 array (size=7) 0 => int 1 1 => int 4 2 => int 45 3 => int 52 4 => int 57 5 => int 64 6 => int 70 array (size=5) 0 => int 2 1 => int 12 2 => int 17 3 => int 74 4 => int 94 array (size=5) 0 => int 14 1 => int 21 2 => int 43 3 => int 57 4 => int 88 array (size=6) 0 => int 5 1 => int 17 2 => int 78 3 => int 85 4 => int 93 5 => int 99 array (size=7) 0 => int 20 1 => int 26 2 => int 59 3 => int 60 4 => int 62 5 => int 63 6 => int 89 array (size=4) 0 => int 6 1 => int 49 2 => int 84 3 => int 85 array (size=5) 0 => int 1 1 => int 7 2 => int 28 3 => int 64 4 => int 70 array (size=5) 0 => int 5 1 => int 19 2 => int 48 3 => int 58 4 => int 85 array (size=3) 0 => int 19 1 => int 55 2 => int 77 array (size=6) 0 => int 15 1 => int 16 2 => int 36 3 => int 79 4 => int 83 5 => int 98 array (size=3) 0 => int 10 1 => int 36 2 => int 49 array (size=7) 0 => int 2 1 => int 3 2 => int 6 3 => int 13 4 => int 14 5 => int 52 6 => int 66 array (size=5) 0 => int 16 1 => int 36 2 => int 70 3 => int 71 4 => int 95 array (size=7) 0 => int 1 1 => int 16 2 => int 21 3 => int 61 4 => int 78 5 => int 91 6 => int 97 array (size=7) 0 => int 29 1 => int 43 2 => int 62 3 => int 67 4 => int 74 5 => int 90 6 => int 99 array (size=7) 0 => int 12 1 => int 30 2 => int 37 3 => int 46 4 => int 62 5 => int 66 6 => int 99 array (size=7) 0 => int 6 1 => int 12 2 => int 40 3 => int 41 4 => int 82 5 => int 95 6 => int 100 array (size=4) 0 => int 37 1 => int 89 2 => int 98 3 => int 100 array (size=5) 0 => int 19 1 => int 25 2 => int 26 3 => int 56 4 => int 59 array (size=5) 0 => int 20 1 => int 27 2 => int 34 3 => int 84 4 => int 93 array (size=7) 0 => int 1 1 => int 24 2 => int 36 3 => int 44 4 => int 68 5 => int 70 6 => int 81 array (size=4) 0 => int 46 1 => int 52 2 => int 60 3 => int 84 array (size=6) 0 => int 1 1 => int 46 2 => int 82 3 => int 86 4 => int 89 5 => int 99 array (size=5) 0 => int 39 1 => int 51 2 => int 53 3 => int 66 4 => int 91 array (size=4) 0 => int 22 1 => int 46 2 => int 75 3 => int 96 array (size=3) 0 => int 72 1 => int 85 2 => int 91 array (size=3) 0 => int 17 1 => int 82 2 => int 97 array (size=4) 0 => int 2 1 => int 8 2 => int 43 3 => int 83 array (size=5) 0 => int 3 1 => int 8 2 => int 14 3 => int 68 4 => int 78 array (size=7) 0 => int 3 1 => int 5 2 => int 12 3 => int 63 4 => int 72 5 => int 89 6 => int 94 array (size=5) 0 => int 14 1 => int 45 2 => int 70 3 => int 73 4 => int 95 array (size=5) 0 => int 27 1 => int 33 2 => int 38 3 => int 75 4 => int 97 array (size=7) 0 => int 7 1 => int 19 2 => int 61 3 => int 66 4 => int 76 5 => int 79 6 => int 80 array (size=7) 0 => int 15 1 => int 22 2 => int 51 3 => int 73 4 => int 84 5 => int 87 6 => int 89 array (size=6) 0 => int 13 1 => int 46 2 => int 64 3 => int 65 4 => int 85 5 => int 97 array (size=4) 0 => int 15 1 => int 31 2 => int 56 3 => int 93 array (size=5) 0 => int 11 1 => int 32 2 => int 69 3 => int 88 4 => int 89 array (size=3) 0 => int 18 1 => int 81 2 => int 96 array (size=4) 0 => int 52 1 => int 56 2 => int 58 3 => int 98 array (size=5) 0 => int 4 1 => int 25 2 => int 30 3 => int 84 4 => int 88 array (size=6) 0 => int 15 1 => int 40 2 => int 59 3 => int 60 4 => int 68 5 => int 71 array (size=6) 0 => int 53 1 => int 65 2 => int 67 3 => int 77 4 => int 83 5 => int 84 array (size=4) 0 => int 43 1 => int 48 2 => int 70 3 => int 99 array (size=5) 0 => int 22 1 => int 47 2 => int 48 3 => int 68 4 => int 74 array (size=6) 0 => int 2 1 => int 5 2 => int 31 3 => int 72 4 => int 76 5 => int 86 array (size=3) 0 => int 52 1 => int 57 2 => int 71 array (size=6) 0 => int 25 1 => int 28 2 => int 62 3 => int 68 4 => int 74 5 => int 80 array (size=7) 0 => int 35 1 => int 40 2 => int 51 3 => int 61 4 => int 63 5 => int 66 6 => int 67 array (size=3) 0 => int 79 1 => int 96 2 => int 100 Array (  => Array (  => 57,62,75 )  => Array (  => 25,40,59,70,71 )  => Array (  => 24,34,48,52,55,71,94 )  => Array (  => 10,14,19,57,78,82,99 )  => Array (  => 3,4,61,84 )  => Array (  => 9,15,17,30,71,94 )  => Array (  => 4,7,17,33,34,70,86 )  => Array (  => 1,28,73,84 )  => Array (  => 53,61,73,77,100 )  => Array (  => 11,15,20,38,81,89 )  => Array (  => 52,56,73,78,86,99 )  => Array (  => 37,46,68,75,81 )  => Array (  => 26,49,91,92 )  => Array (  => 9,21,55,80,89,90,97 )  => Array (  => 6,10,27,40,48,53 )  => Array (  => 4,8,58,65,83,97,98 )  => Array (  => 9,36,37,40,62,89 )  => Array (  => 20,32,76 )  => Array (  => 25,39,43,54 )  => Array (  => 8,46,47,58,96 )  => Array (  => 29,36,58,68,73 )  => Array (  => 3,6,11,16,21,53 )  => Array (  => 9,10,80,91,93,96,97 )  => Array (  => 1,29,46 )  => Array (  => 11,15,20,35,58,99 )  => Array (  => 69,72,83 )  => Array (  => 37,44,49,64,66 )  => Array (  => 15,43,47,94 )  => Array (  => 7,71,82 )  => Array (  => 4,69,74 )  => Array (  => 16,23,42,44,60,83 )  => Array (  => 6,17,31,41,60,65,87 )  => Array (  => 12,28,34,41,46,53,62 )  => Array (  => 3,15,100 )  => Array (  => 37,54,89 )  => Array (  => 14,41,51,59 )  => Array (  => 19,21,40,48,53,73,83 )  => Array (  => 15,37,56,60,65,97 )  => Array (  => 23,40,59,92,97 )  => Array (  => 24,51,68,80 )  => Array (  => 11,52,81 )  => Array (  => 16,18,33,40,44 )  => Array (  => 28,31,41,61,72,87 )  => Array (  => 2,5,68 )  => Array (  => 31,32,33,72,78,87,97 )  => Array (  => 5,18,39,88,96,97 )  => Array (  => 1,40,56,93 )  => Array (  => 6,15,30,58,65,72,79 )  => Array (  => 5,10,43,62,88 )  => Array (  => 49,53,64,75,86 )  => Array (  => 8,37,55,66,74,82 )  => Array (  => 1,4,45,52,57,64,70 )  => Array (  => 2,12,17,74,94 )  => Array (  => 14,21,43,57,88 )  => Array (  => 5,17,78,85,93,99 )  => Array (  => 20,26,59,60,62,63,89 )  => Array (  => 6,49,84,85 )  => Array (  => 1,7,28,64,70 )  => Array (  => 5,19,48,58,85 )  => Array (  => 19,55,77 )  => Array (  => 15,16,36,79,83,98 )  => Array (  => 10,36,49 )  => Array (  => 2,3,6,13,14,52,66 )  => Array (  => 16,36,70,71,95 )  => Array (  => 1,16,21,61,78,91,97 )  => Array (  => 29,43,62,67,74,90,99 )  => Array (  => 12,30,37,46,62,66,99 )  => Array (  => 6,12,40,41,82,95,100 )  => Array (  => 37,89,98,100 )  => Array (  => 19,25,26,56,59 )  => Array (  => 20,27,34,84,93 )  => Array (  => 1,24,36,44,68,70,81 )  => Array (  => 46,52,60,84 )  => Array (  => 1,46,82,86,89,99 )  => Array (  => 39,51,53,66,91 )  => Array (  => 22,46,75,96 )  => Array (  => 72,85,91 )  => Array (  => 17,82,97 )  => Array (  => 2,8,43,83 )  => Array (  => 3,8,14,68,78 )  => Array (  => 3,5,12,63,72,89,94 )  => Array (  => 14,45,70,73,95 )  => Array (  => 27,33,38,75,97 )  => Array (  => 7,19,61,66,76,79,80 )  => Array (  => 15,22,51,73,84,87,89 )  => Array (  => 13,46,64,65,85,97 )  => Array (  => 15,31,56,93 )  => Array (  => 11,32,69,88,89 )  => Array (  => 18,81,96 )  => Array (  => 52,56,58,98 )  => Array (  => 4,25,30,84,88 )  => Array (  => 15,40,59,60,68,71 )  => Array (  => 53,65,67,77,83,84 )  => Array (  => 43,48,70,99 )  => Array (  => 22,47,48,68,74 )  => Array (  => 2,5,31,72,76,86 )  => Array (  => 52,57,71 )  => Array (  => 25,28,62,68,74,80 )  => Array (  => 35,40,51,61,63,66,67 )  => Array (  => 79,96,100 ) ) ``````