PHP VERSION: 8.2.18
random_bytes_random_int.php
<?php
echo '<pre>';
// random_bytes(int length);
$randomStr = random_bytes(16);
// output
$str = '';
$hex = '';
for ($x = 0; $x < strlen($randomStr); $x++) {
$str .= (ctype_print($randomStr[$x])) ? $randomStr[$x] : '.';
$hex .= sprintf('%02x ', ord($randomStr[$x]));
}
echo $str . ' : ' . $hex . PHP_EOL;
$test = array();
printf("\n%20s : %20s\n", 'random_int()', 'rand()');
for ($x = 0; $x < 100; $x++) {
// random_int(int min, int max);
printf("%20d : %20d\n", random_int(1, 3), rand(1, 3));
}
echo '</pre>';
Output
]./A..}...Q..\.\ : 5d 0a 2f 41 85 96 7d 91 e1 cd 51 9a 13 5c c6 5c
random_int() : rand()
1 : 1
3 : 2
2 : 3
2 : 2
2 : 1
3 : 2
3 : 3
3 : 2
2 : 1
1 : 2
1 : 3
3 : 1
1 : 3
3 : 1
2 : 1
3 : 1
2 : 3
2 : 1
2 : 2
2 : 1
3 : 2
2 : 3
1 : 1
3 : 2
3 : 3
2 : 2
3 : 1
1 : 2
3 : 1
1 : 2
2 : 3
1 : 1
3 : 3
1 : 3
2 : 3
1 : 2
3 : 2
1 : 2
3 : 2
1 : 2
2 : 2
1 : 1
1 : 3
3 : 1
2 : 3
2 : 1
3 : 2
2 : 1
2 : 1
2 : 3
1 : 2
3 : 2
1 : 2
2 : 3
1 : 2
1 : 2
3 : 2
1 : 2
1 : 2
1 : 1
3 : 2
1 : 3
1 : 2
2 : 3
3 : 3
2 : 1
1 : 3
3 : 3
1 : 1
1 : 1
1 : 1
2 : 2
2 : 2
3 : 3
1 : 3
3 : 3
3 : 3
2 : 1
2 : 3
3 : 3
2 : 2
3 : 3
3 : 1
2 : 2
2 : 3
3 : 3
1 : 3
3 : 2
2 : 3
2 : 3
2 : 1
3 : 1
3 : 1
3 : 3
2 : 2
1 : 2
3 : 3
3 : 3
3 : 1
3 : 1
SOURCE CODE