1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<h1>Test de array_rand()</h1>
<pre>
<?php
require 'Benchmark/Timer.php';
$arrItem = str_split('abcdefghi', 1);

function oldRandom ($arrItem)
{

    $itemList = array();
    if (isset($arrItem)) {
        $items_selected_nbr = sizeof($arrItem);

        if ($items_selected_nbr > 2) {

            srand((double) microtime() * 1000000);
            $itemList[0] = rand(0, ($items_selected_nbr) - 1);

            do {
                srand((double) microtime() * 1000000);
                $rand = rand(0, ($items_selected_nbr) - 1);
            } while (in_array($rand, $itemList));
            $itemList[1] = $rand;

            do {
                srand((double) microtime() * 1000000);
                $rand = rand(0, ($items_selected_nbr) - 1);
            } while (in_array($rand, $itemList));
            $itemList[2] = $rand;


            $itemList[0] = $arrItem[$itemList[0]];
            $itemList[1] = $arrItem[$itemList[1]];
            $itemList[2] = $arrItem[$itemList[2]];
        }
    }
    return $itemList;
}


function newRandom ($arrItem, $cnt = 3)
{

    return array_intersect_key($arrItem, array_flip(array_rand($arrItem, min($cnt, count($arrItem)))));

}


function getRandomElement (array $tab, $nb)
{

    shuffle($tab);
    $result = array_chunk($tab, $nb);
    return $result[0];
}


?>
</pre>
<h2>Je teste</h2>
<pre>
<?php

var_export(oldRandom($arrItem));
var_export(newRandom($arrItem, 3));
var_export(getRandomElement($arrItem, 3));

?>
</pre>
<h2>J'en demande plus que disponible</h2>
<pre>
<?php
var_export(oldRandom(array_slice($arrItem, 0, 2)));
var_export(newRandom(array_slice($arrItem, 0, 2), 3));
var_export(getRandomElement(array_slice($arrItem, 0, 2), 3));

$timer = new Benchmark_Timer();
$timer->start();
$amount = 500;

$timer->setMarker('oldRandom');
$timer->timeElapsed('Start', 'oldRandom') . "\n";
for ($i = 0; $i < $amount; $i ++)
    oldRandom($arrItem);
$timer->timeElapsed('End', 'oldRandom') . "\n";

$timer->setMarker('newRandom');
$timer->timeElapsed('Start', 'newRandom') . "\n";
for ($i = 0; $i < $amount; $i ++)
    newRandom($arrItem, 3);
$timer->timeElapsed('End', 'newRandom') . "\n";

$timer->setMarker('getRandomElement');
$timer->timeElapsed('Start', 'getRandomElement') . "\n";
for ($i = 0; $i < $amount; $i ++)
    getRandomElement($arrItem, 3);
$timer->timeElapsed('End', 'getRandomElement') . "\n";
$timer->timeElapsed('Start', 'oldRandom') . "\n";
for ($i = 0; $i < $amount; $i ++)
    oldRandom($arrItem);
$timer->timeElapsed('End', 'oldRandom') . "\n";

$timer->setMarker('newRandom');
$timer->timeElapsed('Start', 'newRandom') . "\n";
for ($i = 0; $i < $amount; $i ++)
    newRandom($arrItem, 3);
$timer->timeElapsed('End', 'newRandom') . "\n";

$timer->setMarker('getRandomElement');
$timer->timeElapsed('Start', 'getRandomElement') . "\n";
for ($i = 0; $i < $amount; $i ++)
    getRandomElement($arrItem, 3);
$timer->timeElapsed('End', 'getRandomElement') . "\n";

$timer->stop();
$timer->display();

$start = microtime(true);

?>
</pre>
<hr />
<?php


highlight_file(__FILE__);
?>

History for ZF_experiences/trunk/Php/array/array_rand.php

Diff revisions: vs.
Revision Author Commited Message
20 Diff Diff moosh picture moosh Sat 13 Feb, 2010 14:38:15 +0000

complete with shuffle

19 Diff Diff moosh picture moosh Fri 22 Jan, 2010 06:39:44 +0000

patch some errors

18 Diff Diff moosh picture moosh Fri 22 Jan, 2010 06:17:32 +0000

dump source

17 moosh picture moosh Fri 22 Jan, 2010 06:12:38 +0000