note 113981 added to function.srand
| From: | simon at labs dot coop | Date: | Wed, 25 Dec 2013 09:19:45 +0000 |
| Subject: | note 113981 added to function.srand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-198023@lists.php.net to get a copy of this message | ||
I canât stress how important it is to seed your randomisation process in code! better still
something we found in the BBS Days was if we didnât seed from a token from outside our systems
abstraction layer we would go in circles and so would our users. Here at chronolabs we offer a feed
of randomly changing token on each impression, it also randomly displays a different number of them
this is from http://seed.feeds.labs.coop in the example
below I use DOM to load the XML, Extract the randomisation tokens and then with mt_srand and srand
seed the random selecting processes! The following function when you call it will seed your random
selection process in both the old and new random selection routines all you need to do is call the
function! This will work with any version of PHP 5 and any earlier with DOM Objectivity.
function makeRandomSeeded() {
$file = 'http://seed.feeds.labs.coop/';
$doc = new DOMDocument();
$doc->loadHTMLFile($file);
$skip = array('This feed can', 'Current mode is');
$elements = $doc->getElementsByTagName('description');
foreach($elements as $element) {
$seed = $element->nodeValue;
$found = false;
foreach($skip as $find) {
if (substr($seed, 0, strlen($find))==$find) {
$found = true;
}
}
if ($found==false)
$seeds[] = $seed;
}
shuffle($seeds);
mt_srand($seeds[mt_rand(0, count($seeds)-1)]);
srand($seeds[mt_rand(0, count($seeds)-1)]);
}
Remember when PHP says an integer this also include any character of the Ascii chart if you would
like to see an example of this do the following:
<?php
$a = "000A";
while($a!="001B") {
echo $a;
$a++;
}
?>
----
Server IP: 72.52.91.14
Probable Submitter: 72.52.91.14
----
Manual Page -- http://php.net/manual/en/function.srand.php
Edit -- https://master.php.net/note/edit/113981
Del: integrated -- https://master.php.net/note/delete/113981/integrated
Del: useless -- https://master.php.net/note/delete/113981/useless
Del: bad code -- https://master.php.net/note/delete/113981/bad+code
Del: spam -- https://master.php.net/note/delete/113981/spam
Del: non-english -- https://master.php.net/note/delete/113981/non-english
Del: in docs -- https://master.php.net/note/delete/113981/in+docs
Del: other reasons-- https://master.php.net/note/delete/113981
Reject -- https://master.php.net/note/reject/113981
Search -- https://master.php.net/manage/user-notes.php