note 113980 added to function.mt-srand

From: Date: Wed, 25 Dec 2013 09:19:11 +0000
Subject: note 113980 added to function.mt-srand
Groups: php.notes 
Request: Send a blank email to php-notes+get-198022@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.mt-srand.php Edit -- https://master.php.net/note/edit/113980 Del: integrated -- https://master.php.net/note/delete/113980/integrated Del: useless -- https://master.php.net/note/delete/113980/useless Del: bad code -- https://master.php.net/note/delete/113980/bad+code Del: spam -- https://master.php.net/note/delete/113980/spam Del: non-english -- https://master.php.net/note/delete/113980/non-english Del: in docs -- https://master.php.net/note/delete/113980/in+docs Del: other reasons-- https://master.php.net/note/delete/113980 Reject -- https://master.php.net/note/reject/113980 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#198022) next »