note 109121 added to function.rand
| From: | kevin at schinkenbraten dot de | Date: | Thu, 21 Jun 2012 18:39:14 +0000 |
| Subject: | note 109121 added to function.rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-188739@lists.php.net to get a copy of this message | ||
<?php
/*
* These random number methods are compared by
* the GD-Lib. It shows how random these numbers
* are really. The worst random generator is
* rand().
*
* Usage:
* index.php?type=mt mt_rand()
* index.php?type=md5 rnd()
* index.php?type=lfrs lfrs()
* index.php rand()
*/
/* Config */
$size = 1000; // Size of the Image
$counts = 100000; // Counts of Pixels
$bits = 32; // Bits in the LFRS
/* Image creation */
header("Content-type: image/png");
$im = @imagecreate($size, $size);
$background_color = imagecolorallocate($im, 255, 255, 255);
$pixel = imagecolorallocate($im, 0, 0, 0);
$size_len = strlen($size) - 1;
$chars = array("a", "b", "c", "d", "e",
"f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q",
"r", "s", "t", "u", "v", "w",
"x", "y", "z");
$shift_register = 1;
$mask = 0x02300282;
/* Functions */
function rnd($i) {
/* This is a random number by microtime()
* with a startvalue ($i). This output gets
* hashed with md5() and the chars will all
* be deleted
*/
global $chars, $size_len;
$x = md5($i . time());
foreach ($chars AS $z) {
$x = str_replace($z, "", $x);
}
return substr($x, 0, $size_len);
}
function lfrs() {
/* This is a random number by a LFRS
* (linear feedback shift register) it
* is based on a startvalue and a bit-wise
* shift with XOR and AND.
*/
global $shift_register, $mask;
if ($shift_register & 0x00000001) {
$shift_register = ($shift_register ^ $mask)>>1 | $shift_register[1];
return 1;
} else {
$shift_register>>=1;
return 0;
}
}
/* Execution */
if (isset($_GET["type"]) && $_GET["type"] == "mt") {
// Random Numbers by mt_rand()
for ($i = 0; $i < $counts; $i++) {
imagesetpixel($im, mt_rand(0, $size), mt_rand(0, $size), $pixel);
}
} elseif (isset($_GET["type"]) && $_GET["type"] == "md5") {
// Random Numbers by rnd()
for ($i = 0; $i < $counts; $i++) {
imagesetpixel($im, rnd($i), rnd($i + $counts), $pixel);
}
} elseif (isset($_GET["type"]) && $_GET["type"] == "lfrs") {
// Random Numbers by lfrs()
for ($i = 0; $i < $counts; $i++) {
$bit = "";
for ($j = 0; $j < $bits; $j++)
$bit .= lfrs();
$ax = substr(base_convert($bit, 2, 10), 5, $size_len);
$bit = "";
for ($j = 0; $j < $bits; $j++)
$bit .= lfrs();
$ay = substr(base_convert($bit, 2, 10), 5, $size_len);
imagesetpixel($im, $ax, $ay, $pixel);
}
} else {
// Random Numbers by rand()
for ($i = 0; $i < $counts; $i++) {
imagesetpixel($im, rand(0, $size), rand(0, $size), $pixel);
}
}
imagepng($im);
?>
----
Server IP: 212.124.37.9
Probable Submitter: 91.62.198.28
----
Manual Page -- http://www.php.net/manual/en/function.rand.php
Edit -- https://master.php.net/note/edit/109121
Del: integrated -- https://master.php.net/note/delete/109121/integrated
Del: useless -- https://master.php.net/note/delete/109121/useless
Del: bad code -- https://master.php.net/note/delete/109121/bad+code
Del: spam -- https://master.php.net/note/delete/109121/spam
Del: non-english -- https://master.php.net/note/delete/109121/non-english
Del: in docs -- https://master.php.net/note/delete/109121/in+docs
Del: other reasons-- https://master.php.net/note/delete/109121
Reject -- https://master.php.net/note/reject/109121
Search -- https://master.php.net/manage/user-notes.php