Re: Predictable-random array

From: Date: Tue, 21 Jun 2005 18:54:23 +0000
Subject: Re: Predictable-random array
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-217344@lists.php.net to get a copy of this message
Dotan Cohen wrote:
On 6/21/05, Jochem Maas <jochem@iamjochem.com> wrote:
Dotan Cohen wrote:
....
$items is a static file, that does not change over time. It just needs
that makes things easier :-)
to be output in a reproducable 'random' order for different purposes. I looked over your suggested fuctions, but decided to try Marek's simpler seed solution first. And it worked for what I need, so I will
indeed his is most to the point!
be going that route. But I very much appreciate your insight. Although complicated, it presents a different way of solving the same problem.
the idea I proposed was more of a way to genericly (spelling?!?) hook in different 'random' ordering mechanisms - Marek's suggestion for ordering could be implemented as a callback function in my example. e.g. <?php $items = array(
    /*     |--char 7     */
    "cherries",
    "bananas",
    "orangejuice",
    "red apples",
    "smelly fish",
    "more bananas",
    "ship full of bananas",
    "tarantula",
    "poisoned apple",
    /*     |--char 7     */
); function permaRandom($items, $callback) {
    /* caching left as an exercise to the reader ;-/ */
    if (is_callable($callback)) {
        $items2 = /*($yes = gotcachedpermarandom($callback))
            ? getfromcache($callback)
            : */call_user_func($callback, $items);
        /* if ($yes) storeincache($callback, $items2); */
    }
    return $items2;
} function sortAlphaChar7Random($items) {
    $keys = $items2 = array();
    // assumption made that $item is a string
    // and at least 7 chars long, oh and spaces
    // wont do much good for the sorting probably..
    foreach ($items as $k => $item) {
        $keys[$k] = @$item{7};
    }
    asort($keys, SORT_LOCALE_STRING | SORT_STRING);
    foreach ($keys as $k => $v) {
        $items2[] = $items[$k];
    }
    return $items2;
} function fixedShuffle1($items) {
     static $fixedSeed = 384884;
     $keys = $items2 = array();
     // not sure whether seeding like this is a good idea
     // with regard to other, non-related code that relies
     // on the random generator... but anyway!
     foreach ($items as $k => $item) {
         $keys[] = $k;
     }
     srand( $fixedSeed );
     shuffle($keys);
     // srand( rand() ); // crap 'fix'?
     foreach ($keys as $k => $v) {
         $items2[] = $items[$k];
     }
     return $items2;
} var_dump(permaRandom($items, "sortAlphaChar7Random"), permaRandom($items, "sortAlphaChar7Random"), permaRandom($items, "fixedShuffle1"), permaRandom($items, "fixedShuffle1")); ?>
That in itself is interesting. I intend to play with it a little when I have some more time. Maybe I will find an advantage in your solution
there may be absolutely no advantage ;-) I was mostly trying to cram as much learning material into the idea/example as possible - kind of over engineering (in so far as you could call it engineering) in order to show as many (hopefully) new ideas to who ever read it :-) I'll be interested to know how you get on. rgds, Jochem
that I may need some day! Thank you. Dotan http://english-lyrics.com/ Song Lyrics


« previous php.general (#217344) next »