|
Posted by Dotan Cohen on 10/20/48 11:19
On 6/21/05, Jochem Maas <jochem@iamjochem.com> wrote:
> Dotan Cohen wrote:
> > I've got an array $items which has about 200 entires. I want to print
>
> it might be helpful to know where the array comes from.
> or maybe its a static definition to start with (nothing wrong with that as such :-)
> does $items change over time? in what way?
>
> > them out in random order, but as usual, I have a catch! I need to add
> > a 'key' so that if need be, I can always return to the same 'random'
> > order, like so:
> >
> > $items=predicatable_random($items, "qwerty");
>
> the problem is persistence, if you want a random order that doesn't change
> then you have to create the random order, and then store an index for it
> somewhere. it may be feasable to either serialize the result and read it from disk
> when needed, or store a list of keys (an index of sorts) in file/db and
> use that to perform an ordering as required.
>
> >
> > so for each place that I need the items rearranged, I will replace
> > qwerty with a different string, and get a different 'random' order. I
> > haven't played around with it much as I stumbled into a brick wall
> > fairly early and can't get much going. Any ideas?
> >
> > I tried to play with usort, uksort, ksort, and various combinations
> > with shuffle. The 'randomization' can be based on qualities of each
> > string, such as 'sort alphebetically by the 7th character', so I
>
> thats not random. maybe the 'key' you need to pass is
> a callback function whose job it is to produce a replicable
> (or random for that matter!) ordering somehow - depending on the
> complexity of the calculation and size of the dataset you may
> need to cache (look at http://php.net/mt_srand if you need predictable
> [psuedo]randomness without cacheing (using 'fixed' seeds), and check
> the usernotes on http://php.net/shuffle also). e.g:
>
> $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;
> }
>
> var_dump(permaRandom($items, "sortAlphaChar7Random"),
> permaRandom($items, "sortAlphaChar7Random"),
> permaRandom($items, "sortAlphaChar7Random"));
>
> > thought that I'd try that, but I got lost in explodes and such.
> >
> > Thanks all. I always look forward to my php lessons!
> >
> > Dotan
> > http://english-lyrics.com/el/index.php
> > Song Lyrics
> >
>
>
$items is a static file, that does not change over time. It just needs
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
be going that route. But I very much appreciate your insight. Although
complicated, it presents a different way of solving the same problem.
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
that I may need some day! Thank you.
Dotan
http://english-lyrics.com/
Song Lyrics
Navigation:
[Reply to this message]
|