1
wishcraft
Seed your randomisation selection in modules!! Important!!

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 fromhttp://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 that randomisation from a seed source tokens to make selection better
 */
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($seed0strlen($find))==$find) {
                
$found true;
            }
        }
        if (
$found==false)
            
$seeds[] = $seed;
    }
    
shuffle($seeds);
    
mt_srand($seeds[mt_rand(0count($seeds)-1)]);
    
srand($seeds[mt_rand(0count($seeds)-1)]);
}

2
wishcraft
Re: Ultimate Seed with DOM and seeding Feed from Chronolabs

This is the Ultimate DOM Randomisation method with the seeding feed (http://seed.feeds.labs.coop) for seeding your randomisation! which is very important:

/**
 * Function: makeRandomSeeded() - Seeds the Selection Process of Random Sorting & Selection
 * 
 * @param $filename string - Source of Seeding XML
 * @return integer - the seed used     
 */
function makeRandomSeeded($filename 'http://seed.feeds.labs.coop/') {
    
error_reporting(E_ERROR);
    
    
// Loads XML by forcing loadXML with DOM 
    
$xml file_get_contents($filename);
    
$doc = new DOMDocument();
    
$doc->loadXML($xml);

    
// Parses Description Tag and Remove help and File Descriptor
    
$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($seed0strlen($find))==$find) {
                
$found true;
            }
        }
        if (
$found==false)
            
$seeds[] = $seed;
    }
    
    
// Selects Seed from $seeds
    
shuffle($seeds);
    
$seed $seeds[mt_rand(0count($seeds)-1)];
    unset(
$seeds);
    
    
// Makes a Double Precision Number or Floating Point
    
$parts explode(' '$seed);
    foreach(
$parts as $key => $value)
        if (!
is_numeric($value) && is_string($value))
            unset(
$parts[$key]);
    
shuffle($parts);
    
$keys array_keys($parts);
    switch((string)
mt_rand(0,11)) {
        default:
            
$float = ($parts[$keys[mt_rand(0count($keys)-1)]] * sqrt(microtime(true)));
            break;
        case 
"1":
            
$float = ($parts[$keys[mt_rand(0count($keys)-1)]] / sqrt(microtime(true)));
            break;
        case 
"2":
            
$float = ($parts[$keys[mt_rand(0count($keys)-1)]] - sqrt(microtime(true)));
            break;
        case 
"3":
            
$float = ($parts[$keys[mt_rand(0count($keys)-1)]] + sqrt(microtime(true)));
            break;
        case 
"4":
            
$float = (sqrt($parts[$keys[mt_rand(0count($keys)-1)]]) * microtime(true));
            break;
        case 
"5":
            
$float = (sqrt($parts[$keys[mt_rand(0count($keys)-1)]]) / microtime(true));
            break;
        case 
"6":
            
$float = (sqrt($parts[$keys[mt_rand(0count($keys)-1)]]) - microtime(true));
            break;
        case 
"7":
            
$float = (sqrt($parts[$keys[mt_rand(0count($keys)-1)]]) + microtime(true));
            break;
        case 
"8":
            
$float = (sqrt($parts[$keys[mt_rand(0count($keys)-1)]]) * sqrt(microtime(true)));
            break;
        case 
"9":
            
$float = (sqrt($parts[$keys[mt_rand(0count($keys)-1)]]) / sqrt(microtime(true)));
            break;
        case 
"10":
            
$float = (sqrt($parts[$keys[mt_rand(0count($keys)-1)]]) - sqrt(microtime(true)));
            break;
        case 
"11":
            
$float = (sqrt($parts[$keys[mt_rand(0count($keys)-1)]]) + sqrt(microtime(true)));
            break;
    }
    unset(
$keys);
    unset(
$parts);
    
    
// Constructs Integer from Floating Point/Double
    
$parts explode("."$float);
    if (
count($parts)==2) {
        switch((string)
mt_rand(0,5)) {
            default:
                
$int = (integer)$float;
                break;
            case 
"1":
                
$int $parts[0] * $parts[1];
                break;
            case 
"2":
                
$int $parts[0] . $parts[1];
                break;
            case 
"3":
                
$int $parts[1] . $parts[0];
                break;
            case 
"4":
                
$int = (integer)($parts[1] / $parts[0]);
                break;
            case 
"5":
                
$int = (integer)($parts[0] / $parts[1]);
                break;
                    
        }
    } else {
        
$int = (integer)$float;
    }
    
    
// Randomise the Seeding with Integer
    
srand($int);
    
mt_srand($int);
    if (!
headers_sent())
        
header('Context-seed: ' sha1($int));
    return 
$int;
}


This will offer you an integer seed rather than parsing the full seed and is more largely based in the shuffle command selecting a number from the seeding token!

3
wishcraft
Re: Ultimate Seed with DOM and seeding Feed with XoopsCache

You can speed it up by caching the token for a period of time with the following routine it uses the XoopsCache object to store the token, between one minute and one hour..

/**
 * Function: makeRandomSeeded() - Seeds the Selection Process of Random Sorting & Selection (Using XoopCache to speed up)
 *
 * @param $filename string - Source of Seeding XML
 * @return integer - the seed used
 */
function makeRandomSeeded($filename 'http://seed.feeds.labs.coop/') {
    
error_reporting(E_ERROR);
    
    include_once 
$GLOBALS['xoops']->path('/class/cache/xoopscache.php');
    if (!
$seeds XoopsCache::read('randomisation_seeds_tokens')) {
        
// Loads XML by forcing loadXML with DOM
        
$xml file_get_contents($filename);
        
$doc = new DOMDocument();
        
$doc->loadXML($xml);
    
        
// Parses Description Tag and Remove help and File Descriptor
        
$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($seed0strlen($find))==$find) {
                    
$found true;
                }
            }
            if (
$found==false)
                
$seeds[] = $seed;
        }
        
XoopsCache::write('randomisation_seeds_tokens'$seedsmt_rand(603600));
    }
    
    
// Selects Seed from $seeds
    
shuffle($seeds);
    
$seed $seeds[mt_rand(0count($seeds)-1)];
    unset(
$seeds);

    
// Makes a Double Precision Number or Floating Point
    
$parts explode(' '$seed);
    foreach(
$parts as $key => $value)
    if (!
is_numeric($value) && is_string($value))
        unset(
$parts[$key]);
    
shuffle($parts);
    
$keys array_keys($parts);
    switch((string)
mt_rand(0,11)) {
        default:
            
$float = ($parts[$keys[mt_rand(0count($keys)-1)]] * sqrt(microtime(true)));
            break;
        case 
"1":
            
$float = ($parts[$keys[mt_rand(0count($keys)-1)]] / sqrt(microtime(true)));
            break;
        case 
"2":
            
$float = ($parts[$keys[mt_rand(0count($keys)-1)]] - sqrt(microtime(true)));
            break;
        case 
"3":
            
$float = ($parts[$keys[mt_rand(0count($keys)-1)]] + sqrt(microtime(true)));
            break;
        case 
"4":
            
$float = (sqrt($parts[$keys[mt_rand(0count($keys)-1)]]) * microtime(true));
            break;
        case 
"5":
            
$float = (sqrt($parts[$keys[mt_rand(0count($keys)-1)]]) / microtime(true));
            break;
        case 
"6":
            
$float = (sqrt($parts[$keys[mt_rand(0count($keys)-1)]]) - microtime(true));
            break;
        case 
"7":
            
$float = (sqrt($parts[$keys[mt_rand(0count($keys)-1)]]) + microtime(true));
            break;
        case 
"8":
            
$float = (sqrt($parts[$keys[mt_rand(0count($keys)-1)]]) * sqrt(microtime(true)));
            break;
        case 
"9":
            
$float = (sqrt($parts[$keys[mt_rand(0count($keys)-1)]]) / sqrt(microtime(true)));
            break;
        case 
"10":
            
$float = (sqrt($parts[$keys[mt_rand(0count($keys)-1)]]) - sqrt(microtime(true)));
            break;
        case 
"11":
            
$float = (sqrt($parts[$keys[mt_rand(0count($keys)-1)]]) + sqrt(microtime(true)));
            break;
    }
    unset(
$keys);
    unset(
$parts);

    
// Constructs Integer from Floating Point/Double
    
$parts explode("."$float);
    if (
count($parts)==2) {
        switch((string)
mt_rand(0,7)) {
            default:
                
$int = (integer)$float;
                break;
            case 
"1":
                
$int $parts[0] * $parts[1];
                break;
            case 
"2":
                
$int $parts[0] . $parts[1];
                break;
            case 
"3":
                
$int $parts[1] . $parts[0];
                break;
            case 
"4":
                
$int = (integer)($parts[1] / $parts[0]);
                break;
            case 
"5":
                
$int = (integer)($parts[0] / $parts[1]);
                break;
            case 
"6":
                
$int = (integer)($parts[1] * $parts[0]);
                break;
            case 
"7":
                
$int = (integer)($parts[0] * $parts[1]);
                break;
                
        }
    } else {
        
$int = (integer)$float;
    }

    
// Randomise the Seeding with Integer
    
if (!headers_sent())
        
header('Context-seed: ' sha1($int));
    return 
$int;
}

// Call Routine to Randomise Seed
mt_srand(makeRandomSeeded());
srand(makeRandomSeeded());

Login

Who's Online

157 user(s) are online (104 user(s) are browsing Support Forums)


Members: 0


Guests: 157


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Apr 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits