xref: /PHP-8.2/ext/standard/tests/strings/004.phpt (revision 782352c5)
1--TEST--
2Testing randomization of shuffle() and str_shuffle().
3--FILE--
4<?php
5function stats($f, $a) {
6    $times = 90000;
7    print "$f\n";
8    ksort($a);
9    foreach($a as $k => $v)
10        print "$k: $v: " . sprintf('%0.3f', $v / $times) . "\n";
11}
12$a = array();
13$times = 90000;
14for ($i = 0; $i < $times; $i++) {
15    $p = range(1,4);
16    shuffle($p);
17    $s = join('', $p);
18    if (empty($a[$s])) $a[$s] = 0;
19    $a[$s]++;
20}
21
22stats('shuffle', $a);
23$a = array();
24$times = 90000;
25for ($i = 0; $i < $times; $i++) {
26    $p = '1234';
27    $s = str_shuffle($p);
28    if (empty($a[$s])) $a[$s] = 0;
29    $a[$s]++;
30}
31
32stats('str_shuffle', $a);
33?>
34--EXPECTREGEX--
35shuffle
361234: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
371243: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
381324: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
391342: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
401423: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
411432: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
422134: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
432143: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
442314: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
452341: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
462413: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
472431: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
483124: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
493142: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
503214: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
513241: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
523412: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
533421: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
544123: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
554132: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
564213: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
574231: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
584312: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
594321: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
60str_shuffle
611234: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
621243: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
631324: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
641342: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
651423: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
661432: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
672134: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
682143: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
692314: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
702341: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
712413: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
722431: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
733124: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
743142: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
753214: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
763241: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
773412: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
783421: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
794123: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
804132: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
814213: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
824231: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
834312: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
844321: [34][0-9][0-9][0-9]: 0.0[3-4][0-9]
85