From: Roland Haeder Date: Fri, 6 Mar 2015 23:36:49 +0000 (+0100) Subject: rand() is weak on some OSes as it only produces random numbers from -32766 to X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=7dd7c5ded2519d46360ec330ae2f46fd8cc21096;p=quix0rs-gnu-social.git rand() is weak on some OSes as it only produces random numbers from -32766 to 32766. So let's get rid of it all together and use mt_rand() (which produces *way* larger numbers on *all* systems). Signed-off-by: Roland Haeder Conflicts: lib/noticeform.php Signed-off-by: Roland Haeder --- diff --git a/lib/noticeform.php b/lib/noticeform.php index e241b5812c..81b39d2203 100644 --- a/lib/noticeform.php +++ b/lib/noticeform.php @@ -103,7 +103,9 @@ class NoticeForm extends Form // Do we have to worry about sub-second race conditions? // XXX: Needs to be above the parent::__construct() call...? - $this->id_suffix = rand(); + // When creating a notice form we don't want to collide with + // possibly existing HTML elements, as naming conventions are similar. + $this->id_suffix = mt_rand(); parent::__construct($action); diff --git a/plugins/LdapCommon/LdapCommon.php b/plugins/LdapCommon/LdapCommon.php index 7d052d5b24..aea1b72ea4 100644 --- a/plugins/LdapCommon/LdapCommon.php +++ b/plugins/LdapCommon/LdapCommon.php @@ -370,7 +370,7 @@ class LdapCommon mt_srand((double)microtime() * 1000000); while( strlen( $str ) < $length ) - $str .= substr( $possible, ( rand() % strlen( $possible ) ), 1 ); + $str .= substr( $possible, ( mt_rand() % strlen( $possible ) ), 1 ); return $str; } diff --git a/scripts/createsim.php b/scripts/createsim.php index a4b7667310..e5b47df6de 100644 --- a/scripts/createsim.php +++ b/scripts/createsim.php @@ -71,7 +71,7 @@ function newGroup($i, $j) // Pick a random user to be the admin - $n = rand(0, max($j - 1, 0)); + $n = mt_rand(0, max($j - 1, 0)); $user = User::getKV('nickname', sprintf('%s%d', $userprefix, $n)); $group = User_group::register(array('nickname' => sprintf('%s%d', $groupprefix, $i), @@ -86,10 +86,10 @@ function newNotice($i, $tagmax) $options = array('scope' => Notice::defaultScope()); - $n = rand(0, $i - 1); + $n = mt_rand(0, $i - 1); $user = User::getKV('nickname', sprintf('%s%d', $userprefix, $n)); - $is_reply = rand(0, 1); + $is_reply = mt_rand(0, 1); $content = testNoticeContent(); @@ -97,30 +97,30 @@ function newNotice($i, $tagmax) $stream = new InboxNoticeStream($user->getProfile(), $user->getProfile()); $notices = $stream->getNotices(0, 20); if ($notices->N > 0) { - $nval = rand(0, $notices->N - 1); + $nval = mt_rand(0, $notices->N - 1); $notices->fetch(); // go to 0th for ($i = 0; $i < $nval; $i++) { $notices->fetch(); } $options['reply_to'] = $notices->id; - $dont_use_nickname = rand(0, 2); + $dont_use_nickname = mt_rand(0, 2); if ($dont_use_nickname != 0) { $rprofile = $notices->getProfile(); $content = "@".$rprofile->nickname." ".$content; } - $private_to_addressees = rand(0, 4); + $private_to_addressees = mt_rand(0, 4); if ($private_to_addressees == 0) { $options['scope'] |= Notice::ADDRESSEE_SCOPE; } } } else { - $is_directed = rand(0, 4); + $is_directed = mt_rand(0, 4); if ($is_directed == 0) { $subs = $user->getSubscribed(0, 100)->fetchAll(); if (count($subs) > 0) { $seen = array(); - $f = rand(0, 9); + $f = mt_rand(0, 9); if ($f <= 6) { $addrs = 1; } else if ($f <= 8) { @@ -129,7 +129,7 @@ function newNotice($i, $tagmax) $addrs = 3; } for ($m = 0; $m < $addrs; $m++) { - $x = rand(0, count($subs) - 1); + $x = mt_rand(0, count($subs) - 1); if ($seen[$x]) { continue; } @@ -140,7 +140,7 @@ function newNotice($i, $tagmax) $rprofile = $subs[$x]; $content = "@".$rprofile->nickname." ".$content; } - $private_to_addressees = rand(0, 4); + $private_to_addressees = mt_rand(0, 4); if ($private_to_addressees == 0) { $options['scope'] |= Notice::ADDRESSEE_SCOPE; } @@ -148,36 +148,36 @@ function newNotice($i, $tagmax) } } - $has_hash = rand(0, 2); + $has_hash = mt_rand(0, 2); if ($has_hash == 0) { - $hashcount = rand(0, 2); + $hashcount = mt_rand(0, 2); for ($j = 0; $j < $hashcount; $j++) { - $h = rand(0, $tagmax); + $h = mt_rand(0, $tagmax); $content .= " #tag{$h}"; } } - $in_group = rand(0, 5); + $in_group = mt_rand(0, 5); if ($in_group == 0) { $groups = $user->getGroups(); if ($groups instanceof User_group) { - $gval = rand(0, $groups->N - 1); + $gval = mt_rand(0, $groups->N - 1); $groups->fetch(); // go to 0th for ($i = 0; $i < $gval; $i++) { $groups->fetch(); } $options['groups'] = array($groups->id); $content = "!".$groups->nickname." ".$content; - $private_to_group = rand(0, 2); + $private_to_group = mt_rand(0, 2); if ($private_to_group == 0) { $options['scope'] |= Notice::GROUP_SCOPE; } } } - $private_to_site = rand(0, 4); + $private_to_site = mt_rand(0, 4); if ($private_to_site == 0) { $options['scope'] |= Notice::SITE_SCOPE; @@ -191,7 +191,7 @@ function newMessage($i) { global $userprefix; - $n = rand(0, $i - 1); + $n = mt_rand(0, $i - 1); $user = User::getKV('nickname', sprintf('%s%d', $userprefix, $n)); $content = testNoticeContent(); @@ -202,7 +202,7 @@ function newMessage($i) return; } - $j = rand(0, count($friends) - 1); + $j = mt_rand(0, count($friends) - 1); $other = $friends[$j]; @@ -212,7 +212,7 @@ function newMessage($i) function newSub($i) { global $userprefix; - $f = rand(0, $i - 1); + $f = mt_rand(0, $i - 1); $fromnick = sprintf('%s%d', $userprefix, $f); @@ -222,7 +222,7 @@ function newSub($i) throw new Exception("Can't find user '$fromnick'."); } - $t = rand(0, $i - 1); + $t = mt_rand(0, $i - 1); if ($t == $f) { $t++; @@ -250,7 +250,7 @@ function newJoin($u, $g) global $userprefix; global $groupprefix; - $userNumber = rand(0, $u - 1); + $userNumber = mt_rand(0, $u - 1); $userNick = sprintf('%s%d', $userprefix, $userNumber); @@ -260,7 +260,7 @@ function newJoin($u, $g) throw new Exception("Can't find user '$fromnick'."); } - $groupNumber = rand(0, $g - 1); + $groupNumber = mt_rand(0, $g - 1); $groupNick = sprintf('%s%d', $groupprefix, $groupNumber); @@ -281,7 +281,7 @@ function newFave($u) global $userprefix; global $groupprefix; - $userNumber = rand(0, $u - 1); + $userNumber = mt_rand(0, $u - 1); $userNick = sprintf('%s%d', $userprefix, $userNumber); @@ -293,7 +293,7 @@ function newFave($u) // NB: it's OK to like your own stuff! - $otherNumber = rand(0, $u - 1); + $otherNumber = mt_rand(0, $u - 1); $otherNick = sprintf('%s%d', $userprefix, $otherNumber); @@ -309,7 +309,7 @@ function newFave($u) return; } - $idx = rand(0, count($notices) - 1); + $idx = mt_rand(0, count($notices) - 1); $notice = $notices[$idx]; @@ -328,7 +328,7 @@ function testNoticeContent() return "test notice content"; } - $cnt = rand(3, 8); + $cnt = mt_rand(3, 8); $ids = array_rand($words, $cnt); @@ -393,7 +393,7 @@ function main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax) for ($i = 0; $i < $events; $i++) { - $e = rand(0, $events); + $e = mt_rand(0, $events); if ($e >= 0 && $e <= $ut) { printfv("$i Creating user $n\n");